Codeigniter Multiple Database Connection Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter Google Recaptcha Form Validation Example
- Codeigniter Delete Multiple Rows using Checkbox Example
- How to Create Dynamic Sitemap in Codeigniter?
- How to implement and use DataTables in CodeIgniter?
- Codeigniter Resize Image and Create Thumbnail Example
- Codeigniter Ajax Form Validation Example
- Codeigniter 3 - Basic CRUD application with MySQL Example with Demo
- Codeigniter Drag and Drop Multiple Image Upload Example
- Codeigniter Dynamic Dependent Dropdown using Ajax Example
- Codeigniter Ajax Infinite Scroll Pagination Example
- Codeigniter Image Upload with Validation Example
- Codeigniter Ajax CRUD Tutorial Example
- How to Use Multiple Database in Laravel?
- Laravel Database Backup using Laravel Backup Package