A Basic Database Connection Manager

The follow is an extremely simplified version of a database connection manager. It allows you to save multiple mysqli connections to make managing a complex environment more simple. There are many ways to implement a database connection manager this is probably one of the most generic and simple way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
    class DatabaseManager {
 
        private static $_instances = array();
 
        public static function getInstance($instance_name, $connection_info = array()) {
            if($connection_info) {
                list($db_host, $db_user, $db_pass, $db_name) = $connection_info;
                self::$_instances[$instance_name] = new MySQLi($db_host, $db_user, $db_pass, $db_name);
            }
            return self::$_instances[$instance_name];
        }
    }
 
    function db($instance_name='default', $connection_info = array()){
        return DatabaseManager::getInstance($instance_name, $connection_info);
    }
?>


If this makes sense you can jump straight to the usage examples

Otherwise let’s take a look at this piece by piece:

1
2
3
4
<?php
    class DatabaseManager {
 
        private static $_instances = array();

The $_instances array will contain an instance of each MySQLi connection object. It needs to be static so that you do not lose the connections you have saved.

6
7
8
9
10
11
12
    public static function getInstance($instance_name, $connection_info = array()) {
            if($connection_info) {
                list($db_host, $db_user, $db_pass, $db_name) = $connection_info;
                self::$_instances[$instance_name] = new MySQLi($db_host, $db_user, $db_pass, $db_name);
            }
            return self::$_instances[$instance_name];
        }

The getInstance() function doesn’t just get, it also acts like a setter. An instance name is required, it is how you will distinguish between your different connections. The connection_info array needs to be set when you want to create a new connection, as long as you have all of the values you need to use the MySQLi object in the array and they are in the proper order, you won’t have any problems.

At the end of the class it returns the desired connection to you. There is no error handling built into this class, though I may add that at a later post.

15
16
17
18
    function db($instance_name='default', $connection_info = array()){
        return DatabaseManager::getInstance($instance_name, $connection_info);
    }
?>

Next I wrote a little wrapper function to make calling the DatabaseManager a simpler task. Normally you would need to do DatabaseManager::getInstance(‘db_foo’); to retrieve your connection. This can get cumbersome and it is much easier to call db(‘db_foo’); instead.

You will also notice that with this db() function we added another feature. If, when you create a database connection, you save its instance name as ‘default’ you will only have to call db() to retrieve the MySQLi link, this makes it even easier.

Now for some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
    // First create a connection, we will use this as our default
    // I choose to instantiate first, and then use because chaining
    // a query call after you pass connection info is messy
    db('default', array('localhost', 'my_db_user', 'my_db_pass', 'my_db_foobar'));
    db('globals', array('localhost', 'my_db_user', 'my_db_pass', 'my_db_globals'));
 
    // Now that the connection is made, I can use it
    // Notice that because I used 'default' as my instance name,
    // I do not have to pass an instance name in when retrieving it
    $results = db()->query('SELECT some_column FROM foobar_table WHERE foo = 1');
 
    $gresults = db('globals')->query('SELECT system_value FROM system_options WHERE system_key = 'foobar');
?>
You can leave a response, or trackback from your own site.
Subscribe to RSS Feed Subscribe to Tumblr Follow me on Twitter!