The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I see a lot of requests for help involving MySQL where database connections are created manually each time the DB needs accessed, whether it be inside a class or function, or just a "loose" file...also see a lot of require("connection.php")
stuff as well.

Thought I'd help out and show one method of creating a portable database connection, implementing PDO as the interface. Note, there are many ways to do this, but most of those ways involve doing crazy and/or questionable things (global variables, singletons, etc ).
The method I'm about to introduce avoids any "code smell", and ensures that you can access your DB from any scope, anywhere, by simply setting the connection details one time, and importing it directly whenever needed.
Shameless plug -- this is taken from a PHP framework that I am working on, located at https://github.com/arout/kwfusion

Not only that, but at the end of this post, I will show you how to use this registry to make practically anything portable, not just your database. This registry application also acts as a dependency injector, so that you can ensure that every module you use has all of it's dependencies met before you use it!

Before you get started, this will require that you have PHP version 5.3 or newer ( which is no problem, since you are using ver 5.5+ anyway, right? Right?? )

As the file name suggests, this will act as a registry -- it will check if you have already registered a module (your database) and will throw an exception if you have not yet registered the requested model. You will only need to register a module one time.

Next, in the same directory as registry.php, create a file named db.php. Include the following in db.php:

Make sure to edit the namespace, as well as entering your DB settings.

How to use:

Importing the file
Ideally, you already have Composer or some other method for autoloading files, in which case this will "just work" after you let your autoloader know where to find these files....otherwise, simply include the db.php file when you need to get to the database.

Working with the database
This is where this script earns it's bacon. Regardless of whether you are inside a class, function, other side of Jupiter, you need not worry about passing dependencies or anything else to get to your database. Simply call it like this:

PHP Code:

\Application::run('Database');

More specifically, here is a working example of how to do it:

PHP Code:

// Store the connection as $db$db = \Application::run('Database');

// Now just use it like any other PDO connection$query = " SELECT something FROM some_table WHERE data = ? ";$result = $db->prepare($q);$result->execute( array( $_POST['blah'] ) );

Give your module a name as the first argument ( in this case, I named my router....Router ).
The line: return new Your\Namespace\Router; is simply instantiating the Router class that you would have created elsewhere.

Then use it like you did the database:

PHP Code:

$router = \Application::run('Router');

This in itself is not very useful if you already have autoloading set up....you can just as easily simply instantiate your class as normal. To recieve it's true value, we would use this as a dependency injection container.
Here is an example of it injecting dependencies ( copy/pasted from my framework ):