Codeigniter Multiple Database Connection Example

By Hardik Savani November 5, 2023 Category : Codeigniter

Today, I want to share with you how to connect multiple databases in PHP CodeIgniter 3 application. it is very easy to configure multiple databases in CodeIgniter app. you can simply add database query, join etc with multiple databases.

As we know well, in today we may need to add multiple databases on our application. all then framework provides multiple database connections. Codeigniter also provide multiple database connections in a single app. We have to simply add database configuration array to database.php file. Then we can simple load specific database data by using "$this->load->database('another_db', TRUE);" help.

Here i explain full example for add multiple connection. So you need to create two database. in this example i created two database with following name:

1)codeig

2)laravel_test

I also created "items" table with above two databases. So let's proceed with additional configuration array.

Step 1: Add Database Configuration

In first step we will add two database configuration in database.php file. one is default and another is for extra that we need for testing. So let's add.

application/config/database.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');


$active_group = 'default';

$query_builder = TRUE;


$db['default'] = array(

'dsn' => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => 'root',

'database' => 'codeig',

'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

);


$db['another_db'] = array(

'dsn' => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => 'root',

'database' => 'laravel_test',

'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

);

Step 2: Add Route

In this step, we will add one route "test_db" for demo, that way when we run this route we will the output, So let's add following route on your routes.php file.

application/config/routes.php

$route['test_db'] = 'welcome/test_db';

Step 4: Add Controller Method

In this step we require to add "test_db" method on welcome controller, So let's add with following code. you have to just copy of welcome.php controller file:

application/controllers/Welcome.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');


class Welcome extends CI_Controller {


public function test_db()

{

$this->load->database();

$query = $this->db->get("items");

echo "<pre>";

print_r($query->result());


$second_DB = $this->load->database('another_db', TRUE);

$query2 = $second_DB->get("items");

print_r($query2->result());

exit;

}

}

I hope it can help you...

Shares