Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:54+00:00 2026-06-11T10:04:54+00:00

I am used to mysql database access using the procedural mysql method. I am

  • 0

I am used to mysql database access using the procedural mysql method. I am a beginner – intermediate programmer.

I am trying to learn the PDO api, however all resources which discuss using PDO show the connection string, username and password.

e.g.

<?php

try {
    $db_conn = new PDO('mysql:host=localhost;dbname=databaseName','username', 'password');  
} 
catch (PDOException $e) {
    echo 'Could not connect to database';
}


$sql = 'SELECT * FROM Products';
$stmt = $db_conn->prepare($sql);

...
...
...

?>

What I want, and think would be better programming is to put my PDO connection into a new file. then where I want to run an SQL query, I require_once(‘PDO.php’) or similar.

The problem I have with this is as follows:

  1. How do I close the connection? Simply $db_conn = null; ??
  2. Should I close the connection after each query is run, then re-open the connection?
  3. Should I close the connection or is it automatically destroyed when the user closes the browser?

I am working from a book called PHP Master: Writing Cutting Edge Code. http://www.sitepoint.com/books/phppro1/ and this has completely omitted any reference to closing the connection / destroying the object after it has been used.

Furthermore, I have looked at online tutorials, and they all connect to the database using PDO inline as opposed to having a separate database connector. This I am not happy with for many reasons:

  1. I have to type username & password to connect every time.
  2. If I get a developer to take a look at code / write some code, they will all have access to the database.
  3. If I change the DB username & Password, then each file which connects to the database will need to be updated.

Could anybody recommend a better resource? Could anybody advise on what is the best practice way to do this?

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:04:55+00:00Added an answer on June 11, 2026 at 10:04 am

    Your question about how to store the database name, username and password have nothing to do with the capabilities of PDO. This is an implementation choice. The way you use to work with procedural functions can also be applied to PDO, the difference is that with PDO you work with objects instead.

    So for simplicity, store the PDO creation of an object, either in a function or class, in which you can create the PDO instance anytime, e.g.

    function createPDO($cfg) {
      try {
        return new PDO("mysql:host=".$cfg['host'].",port:".($cfg['port']).";dbname=".($cfg['name']).";",$cfg['username'], $cfg['password']);
      } catch(PDOException $e) {
        // handle exceptions accordingly
      }
    }
    

    You can centralise these in whatever PHP file you like to include, just like you were used with the procedural functions.

    You have two choices, either put all the relevant database information inside the createPDO, or use something like a config ($cfg) variable to store all this information.

    $config = array();
    $config['db'] = array(
      'host' => 'localhost',
      'name' => 'databse',
      'username' => 'userx',
      'password' => 'passy'
      /* .. etc */
    )
    

    Using the createPDO function would be as followed

    $db_conn = createPDO($config['db']);

    For connections closing, each connection made to the database automatically disconnects after PHP exits its execution. You can however, close the connection if you wish, by setting the variable of the PDO object you assigned it to, in this example (and in yours) $db_conn to null

    $db_conn = null; // connection closed.

    The PDO has a manual http://php.net/manual/en/book.pdo.php here, which is a good start getting to know PDO a bit better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Default code used to LIMIT the results fetched from the MySQL database using
I am trying to setup the connection to a Mysql database. However I am
I've been trying to connect Excel to online mysql database. Im using 5.1 DSN
In my java program I used Hybernate technology to access MySQL database table called
I'm using GWT for a web-app and I need to access to a mySql-database.
I'm trying to connect to a mysql database using Connector/J but get the same
I have a MySQL database includes a table named 'Task'. and I used Hibernate
How get data from mysql database? I use ASP.NET and C#.Previously, I used LINQ
how do i get the storage engine used by a specific database in MySql?
We have installed OpenDataKit (ODK) Aggregate and we are using a MySQL Database. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.