HOW TO LOAD MULTIPLE DATABASES IN CODEIGNITER FRAMEWORK.

Date posted :26/04/2019

LOAD MULTIPLE DATABASES:

Most importantly, codeIgniter has a config file that you can store your database connection values such as username, password, database name.

Therefore this config file located at application/config/database.php.

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'form',
	'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
);
  • hostname – The hostname of your database server. Often this is “localhost”.
  • username – The username used to connect to the database.
  • password – The password used to connect to the database.
  • database – The name of the database you want to connect to.
  • dbdriver – The database type. ie: mysql, postgres,etc. Must be specified in lower case.
  • dbprefix – An optional table prefix which will added to the table name when running  queries of Active Record. This permits multiple CodeIgniter installations to share one database.
  • pconnect – TRUE/FALSE – Whether to use a persistent connection.
  • db_debug – TRUE/FALSE – Whether database errors should be displayed.
  • cache_on – TRUE/FALSE – Whether database query caching is enabled.
  • cachedir – The absolute server path to your database query cache directory.
  • char_set – The character set used in communicating with the database.
  • dbcollat – The character collation used in communicating with the database.

DATABASE CONFIGURATION FILE OF CODEIGNITER

Similarly, to open Database Configuration file, go to application/config/database.php

Firstly, you need to change ‘pconnect’ => FALSE,

MAKE TWO DATABASE CONNECTIVITY SETTINGS

/* ======= First database connectivity Settings =========*/
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'form',
	'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,
	'failover' => array(),
	'save_queries' => TRUE
);
/* ======= Second database connectivity Settings =========*/
$db['database2'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'db2',
	'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,
	'failover' => array(),
	'save_queries' => TRUE
);

NOTE:

Important Setting: As a result, codeigniter cannot connect multiple database in connection. So you need to put a simple setting in system/database/ DB_driver.php

<?php
public function simple_query($sql)
 {
 if ( ! $this->conn_id)
 {
 if ( ! $this->initialize())
 {
 return FALSE;
 }
 }
 $this->db_select(); //Just Add this line here
 
 return $this->_execute($sql);
 } 
 
?>

Conclusion

In this article, I have discussed the problem and the solution to using multiple database connections in CodeIgniter projects.

Thanks for using pheonix solutions.

You find this tutorial helpful? Share with your friends to keep it alive.