How To Connect Multiple Databases In Codeigniter

Connect multiple databases in Codeigniter

Here you can learn how to connect multiple databases in Codeigniter in easy way and you can done within few minutes with not more than 3 steps.

Let’s go into this step by step.

STEP 1 

  • Open your project->application->config->databases.php
  • Add the databases like below.
  • Basically we need to add the copy of the default database which we use usually.
  • Then just need to change the variable name instead of default, to differentiate two databases.
$db['default'] = array(
		'dsn'	=> '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => 'password',
		'database' => 'database_1_name',
		'dbdriver' => 'mysqli',
		'dbprefix' => 'wems_',
		'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['database2'] = array(
		'dsn'	=> '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => 'password',
		'database' => 'database_2_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
	);	

STEP 2 

  • Open your controller & call the model from which you want to get the query data. 
$this->load->model('common_model');
$this->common_model->getQueryDetails();

STEP 3

  • Open your model and write the query.
  • For this we first need to load the second database from which you need to get data.
  • There is no need to load the default database as it is already loaded by default.
$newLoadedDBVariableName = $this->load->database('nameOfConnectionUsedInsteadOfDefault',TRUE);
  • Here $newLoadedDBVariableName  referes to variable name we need to use while calling query instead of $this->db
  • In common for default database we use $this->db->select() to write query, but for new database we use $newLoadedDBVariableName->select()
  • nameOfConnectionUsedInsteadOfDefault refers to the name we used in the database.php file instead of default.
  • Then write the query using this variable declared.

  • Now write the query to get data from the database you want to get.
/////////////////////  For Second Database ////////////////////
public function getQueryDetails()
	{
		$db2 = $this->load->database('database_2_name',TRUE);

		$db2->select('*');
		$db2->from('tableName');
		$query = $db2->get();
        if($query->num_rows() > 0):
           	return $query->result_array();
		endif;
	}	// END OF FUNCTION

/////////////////////  For Default Database ////////////////////
public function getQueryDetails()
	{

		$db2->select('*');
		$db2->from('tableName');
		$query = $db2->get();
        if($query->num_rows() > 0):
           	return $query->result_array();
		endif;
	}	// END OF FUNCTION

These are the steps to be followed to connect multiple databases in codeigniter

In the same way you can connect any number of databases and use them accordingly.

For more updates:

Follow us on our Facebook page :
Facebook

Do Follow my Instagram page for more updates :
Instagram

Leave a Reply

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

The maximum upload file size: 2 MB. You can upload: image, document. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded.