Codeigniter 3 Multiple Database Connection in PHP

Codeigniter 3 Multiple Database Connection in PHP

In this Post We Will Explain About is Codeigniter 3 Multiple Database Connection in PHP With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to Connect Multiple Database in Codeigniter Example

In this post we will show you Best way to implement Multiple Database Connection in CodeIgniter, hear for Create Multiple Database Connections in CodeIgniter with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Multiple Database Connection in PHP Codeigniter 3

First of all i explain full source code with example for Put multiple Database connection. Therefor you simple need to make two mysql database. in this program i make two database or multiple db with following name Like as:

Also Read :   PHP Get ID of Last Inserted Record

1)live24ufirst
2)live24usecond

I also make “products” table with some above two or more databases. Therefor step by step proceed with some advance configuration PHP array.

Phase 1: Add Database Configuration

In this first Phase we will put two or more database setting configuration in main Like as a database.php root file. one is first is default and another is for Additional that we need for some data testing. Therefor let’s Put source code.

application/config/database.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default'; // first default
$query_builder = TRUE; //boolean true or false


$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'Your_host_name',
	'username' => 'your_user_name',
	'password' => 'your_db_password',
	'database' => 'live24ufirst',
	'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['second_db'] = array(
	'dsn'	=> '',
	'hostname' => 'Your_host_name',
	'username' => 'your_user_name',
	'password' => 'your_db_password',
	'database' => 'live24usecond',
	'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
);

Phase 2: Add Route

In this Phase, we will add one route “live24usecond” for demo, that way when we run this route we will the output, Therefor let’s add following route on your routes.php file.

Also Read :   Connect Multiple Databases in CodeIgniter

application/config/routes.php

$route['live24usecond'] = 'homelive/live24usecond';

Phase 4: Add Controller Method

In this Phase we some file require to add “live24usecond” function on homelive Codeigniter controller, Therefor let’s put with following source code. you have to just simple step by step copy and post of Codeigniter homelive.php controller file:

application/controllers/Homelive.php

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


class Homelive extends CI_Controller {
	public function live24usecond()
	{
		$this->load->database();
		$lstquery = $this->db->get("products");
		echo "<pre>";
		print_r($lstquery->result());


		$second_DB = $this->load->database('second_db', TRUE); 
		$secquery = $second_DB->get("products");
		print_r($secquery->result());
		exit;
	}
}

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is Multiple Database Connection in PHP Codeigniter 3 And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.