Connect multiple database and call in code igniter



Hello here is code for connecting multiple database in Code igniter



- First Open database.php file and make below code

For First Database

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

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);


For Second Database 

$active_group = 'ejabberd'; // this must be your second database name
$active_record = TRUE;

$db['ejabberd']['hostname'] = 'localhost';
$db['ejabberd']['username'] = 'root';
$db['ejabberd']['password'] = ''";
$db['ejabberd']['database'] = 'db1;
$db['ejabberd']['dbdriver'] = 'mysqli';
$db['ejabberd']['dbprefix'] = '';
$db['ejabberd']['pconnect'] = FALSE;
$db['ejabberd']['db_debug'] = TRUE;
$db['ejabberd']['cache_on'] = FALSE;
$db['ejabberd']['cachedir'] = '';
$db['ejabberd']['char_set'] = 'utf8';
$db['ejabberd']['dbcollat'] = 'utf8_general_ci';
$db['ejabberd']['swap_pre'] = '';
$db['ejabberd']['autoinit'] = TRUE;
$db['ejabberd']['stricton'] = FALSE;


Now it's time to call database

Add below line in your model

class commonmodel extends CI_Model {
  
   public function __construct()
    {
        parent::__construct();
        
        $this->db = $this->load->database('default', TRUE);
        $this->db1 = $this->load->database('ejabberd', TRUE);
    }
}

Comments

Popular posts from this blog

Call CI controler functiona from View using ajax

Pay using braintree