My Programming Tutorials

My Programming Tutorials

Use 2 or more database in a single Codeigniter project

Last updated on by , no comments

There are circumctances where some developers need to use multiple databases in a single web project.

If you are here then you surely want to learn that How to use multiple database in a codeigniter.

So without any further ado let’s get into the trick.


create another key for $db variable (array) like bellow, you can copy and paste the default db details as well but make sure to change the key to database2.

NOTE : keys should be named as database2, database3 etc.

$db['database2'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE

Load the newly connected database as like bellow

$db2 = $this->load->database('database2', true);

you can use $db2 object to interact with second  database easily.

NOTE : instead of $this->db you have to use $db2 i.e ($db2->select('*));


Author Info

Paritosh Pandey

He loves Technology


Leave a Reply

Your email address will not be published. Required fields are marked *