Installing CodeIgniter
If you don't have CodeIgniter installed yet, you can use
this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this source code.
After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in
htdocs folder of my XAMPP.
Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as
codeigniter_multiple_db so I'm using the below code.
localhost/codeigniter_multiple_db
Creating our Database
Next, we're going to create the databases that we are going to use in this tutorial.
I've included in the downloadable of this tutorial the databases. All you need to do is import them in your phpMyAdmin. If you have no idea on how to import, please refer to my tutorial
How import .sql file to restore MySQL database.
After successful import, we should have two databases named
mydatabase and
mysecond.
Configuring our Database Connection
Next, we're going to connect our codeigniter application to the databases that we created earlier.
1. In your codeigniter app folder, open
database.php located in application/config folder.
2. Create our databases by creating the ff configurations:
For default db
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'save_queries' => TRUE
);
For second db
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mysecond',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'save_queries' => TRUE
);
Configuring our Base URL
Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this.
1. In your codeigniter app folder, open
config.php located in application/config folder.
2. Find and edit the ff line:
$config['base_url'] = 'http://localhost/codeigniter_multiple_db';
where
codeigniter_multiple_db is the name of your app folder.
Creating our Models
Next, we create the models for our databases. Create the ff files in application/models folder of our app.
Members_model.php
This is from our default db.
<?php
class Members_model extends CI_Model {
function __construct(){
parent::__construct();
$this->load->database();
}
public function getUsers(){
$query = $this->db->get('members');
return $query->result();
}
}
?>
Seconddb_model.php
This is from our second db
<?php
class Seconddb_model extends CI_Model {
function __construct(){
parent::__construct();
//load our second db and put in $db2
$this->db2 = $this->load->database('second', TRUE);
}
public function getsecondUsers(){
$query = $this->db2->get('members');
return $query->result();
}
}
?>
Creating our Controller
Next, we create our controller.
Create a file named
Main.php in application/controllers folder of our app and put the ff codes.
<?php
defined('BASEPATH') OR
exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct(){
parent:: __construct();
$this->load->helper('url');
//load default database
$this->load->model('members_model');
//load second database
$this->load->model('seconddb_model');
}
public function index(){
//default database data
$data['firsts'] = $this->members_model->getUsers();
//second database data
$data['seconds'] = $this->seconddb_model->getsecondUsers();
$this->load->view('show', $data);
}
}
Cofiguring our Default Controller
Next, we are going to set our default controller so that whenever we haven't set up a controller to use, this default controller will be used instead.
Open
routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
$route['default_controller'] = 'main';
Creating our View
Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using
this link.
Create the ff files inside application/views folder.
show.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>How to connect to multiple database in CodeIgniter</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">Connecting to Multiple Databases</h1>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<h3>Default Database Data</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
//$data['firsts'] from our controller
foreach($firsts as $first){
?>
<tr>
<td><?php echo $first->id; ?></td>
<td><?php echo $first->firstname; ?></td>
<td><?php echo $first->lastname; ?></td>
<td><?php echo $first->address; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<h3>Second Database Data</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
//$data['seconds'] from our controller
foreach($seconds as $second){
?>
<tr>
<td><?php echo $second->id; ?></td>
<td><?php echo $second->firstname; ?></td>
<td><?php echo $second->lastname; ?></td>
<td><?php echo $second->address; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
That ends thsi tutorial. Happy coding :)