Multiple database connection in codeigniter – onlinecode

Multiple database connection in codeigniter – onlinecode

In this post we will give you information about Multiple database connection in codeigniter – onlinecode. Hear we will give you detail about Multiple database connection in codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

CodeIgniter is MVC based PHP framework, it is possible Multiple database connection in codeigniter. so you can follow below code.

Open the Database.php file

if you want to connect multiple databases in CodeIgniter then open the database.php file. this configuration file available in the array format so you have to do it second database in an array configuration.

there is two connection of the following example for CodeIgniter. first is by default CodeIgniter provide and we will create a second connection. change the database name, database username, and database password in the second connection.

see below example

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

$active_group = 'default';
$query_builder = TRUE;

// codeigniter provide default code
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => '',
	'password' => '',
	'database' => '',
	'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
);

// second connectivity of database
$db['second_db'] = array(
	'dsn'	=> '',
	'hostname' => 'here add second database of hostname',
	'username' => 'here add second database of username',
	'password' => 'here add second database of password',
	'database' => 'here add second database of database name',
	'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
);

?>

The databases configuration after you can access the database.

//access default database
$this->load->database();
$query = $this->db->query('select * from register');

//access the second database
$second_db= $this->load->database('second_db', TRUE);
$query = $second_db->get('select * from register2');

Please follow and like us:

Hope this code and post will helped you for implement Multiple database connection in codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  80  =  86

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US