How to Connect Multiple Database in Codeigniter

If you want to connect multiple database in single application with codeigniter, just follow below simple steps and setup configuration.

Set multiple database details in database.php file

You should provide all the database details in “application/config/database.php” file with defferent groups, like below examples.

Generally, you have to set default database group, like below code :

Now, You will need to add another database details in a $db config array with new DB group index – like ‘db2’.

Now, you can use the both the database in your codeigniter application, you have to load the database and create database object in another variable that you can use in your model for perform database operations.

Connecting Multiple Databases

If you want to connect multiple database simultaneously in your application, you need to load database and create object.

Codeigniter database load function and parameters :

In above function first parameter is database group name which you have define in database.php file and second parameter for return database object or not.

database objects :

Get data from database Using Multiple Database

Get Data from default group databse

Get Data from db2 group databse

Codeigniter model function example with db2 group databse