Sunday, January 27, 2013

Connecting multiple db using CodeIgniter framework:

In Applications/config folder open the database.php file. You will find the default database connection code. Which is need to to be replaced with your settings. Like database name, hostname, username, password, etc… And ‘$active_group’ variable is set with ‘default’ value. You can replace them all but only if you are sure about them.  The ‘Record set’ code would be like this,
       $active_group = 'default';
               $active_record = TRUE;
               $db['default']['hostname'] = 'localhost';
               $db['default']['username'] = 'root';
               $db['default']['password'] = '';
               $db['default']['database'] = 'feedback';
               $db['default']['dbdriver'] = 'mysql';
               $db['default']['dbprefix'] = '';
               $db['default']['pconnect'] = TRUE;
               $db['default']['db_debug'] = TRUE;
               $db['default']['cache_on'] = FALSE;
               $db['default']['cachedir'] = '';
               $db['default']['char_set'] = 'utf8';
               $db['default']['dbcollat'] = 'utf8_general_ci';
               $db['default']['swap_pre'] = '';
               $db['default']['autoinit'] = TRUE;
               $db['default']['stricton'] = FALSE;

          If you want to connect to multiple databases at a time, you can connect by Copying Record set and paste below the code and change the multi-dimensional array key value ‘default’ to ‘new_conn’ i.e

             $db['default']  ==>  $db['comments’]

And as well as the database values. Like hostname, dbname, username, pwd, …

Following is the new database connection array.

//================NEW COMMENTS DATABASE ==========

               $db['comments']['hostname'] = 'localhost';
               $db['comments']['username'] = 'root';
               $db['comments']['password'] = '';
               $db['comments']['database'] = 'comments';
               $db['comments']['dbdriver'] = 'mysql';
               $db['comments']['dbprefix'] = '';
               $db['comments']['pconnect'] = TRUE;
               $db['comments']['db_debug'] = TRUE;
               $db['comments']['cache_on'] = FALSE;
               $db['comments']['cachedir'] = '';
               $db['comments']['char_set'] = 'utf8';
               $db['comments']['dbcollat'] = 'utf8_general_ci';
               $db['comments']['swap_pre'] = '';
               $db['comments']['autoinit'] = TRUE;
               $db['comments']['stricton'] = FALSE;

Connection to new database:

By default the CodeIgniter connection statement is

This code connects the default database which is set following variables,

        $active_group = 'default';

        $active_record = TRUE;

         For connecting to new database you need to specify the database name along with this default connection statement. And you have to set TRUE for $active_record variable, Following is the code snippet:


         First database() method arguments is database connection array key value which will be set to ‘$active_group’ I.e comments which we already defined below the default database connection array. And second argument is TRUE/FALSE which will change ‘$active_record’ variable value.
This connection statement returns the connection results which we storing into a variable. So we can query the database with that variable. Following code is example query statements,

$res=$a->insert("comment ",$data);

You may wish to connect to another new database then follow the same steps to achieve.


  1. thank s it was very usefull for me .

  2. Framework only give the final structure and look for the website with complete mode.I really grateful to getting this information from the blog.
    Web Designing Company Bangalore | Website Designers Bangalore

  3. hey nice source for us, and you have incredible work in this blog for collection the information and i have to bookmark this blog for the future use.

    PHP Web Development Services India

  4. This information gives the newbees an idea to connect various databases through CI. As php web application development services opens the door for fast and accurate website development, there is a lot of need to connect various databses to fetch the information. You informations helps us a lot, Thanks.

  5. If you want to connect multiple database in Codeigniter application please follow below link and configure multiple database setting.

  6. Thanks for sharing such a useful and informative post.
    E-commerce solution provider

  7. Ahaa, its nice conversation on the topic of Connecting multiple db using CodeIgniter framework at this weblog, I have read all that, so at this time me also commenting at this place. Thanks for sharing helpful info with us.

    Responsive Kids Websites Design and Marketing in Affordable Price by OptimiZed360.Com

  8. Thank you for this golden advice you shared here.Thank you for such a great article :)
    Mobile App Development Company Indore

  9. This comment has been removed by the author.

  10. Great tip. However, it should also be explained that developers should make it a practice to make sure to close the database connection manually once the code doesn't require the connection anymore. source:

  11. Blog comment is an effective way to improve your site ranking thru SEO-link building.Yes really this blog commenting sites list is very helpful to me and also all new visitor of this website. I can go to any website by a single click from your blog. Thanks Fran for sharing this kind of helpful article.
    Website Development Services in Bangalore
    Web designer in bangalore

  12. Nice information for me, thanks for sharing this details and i read the whole information and i will bookmark this information for future use.

    Dedicated Codeigniter Developer