Home > Software design >  How to use multiple database in codeigniter?
How to use multiple database in codeigniter?

Time:09-08

I am working on codeigniter and need to use multiple databases in codeigniter. I had checked that we can create multiple database connections in config file. But i need to load diff. tabs in admin panel according to selected state. e.g. when i select state 'A' then database of state 'A' will be used and all tabs will be changed accordingly if i select state 'B' then database of state 'B' will be loaded and admin tabs will be changed accordingly. Is there any way to do this in codeigniter?

CodePudding user response:

You can provide the second database information in application/config/database.php then, you should set the default database group, like so:

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

You can then add another database in a new array. let's call the secondary database as otherdb.

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

the setting of primary database provided in the array named $db['default'] and the secondary provided in the array named $db['otherdb']

Then, when you will use the secondary database don't forget to load like this : $otherdb = $this->load->database('otherdb', TRUE);

CodePudding user response:

As you will see a default database setting in application/config/database.php. As shown below

$db['db1'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'mydatabase',
    '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
);

And now you can add another database setting in the bottom of the same file like this

$db['db2'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'mysecond',
    '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
);

This is how you can use your second db in the Model file.

<?php
    class multipleDB_model extends CI_Model {
        function __construct(){
            parent::__construct();
            //load our the second DB and put in $db2
            $this->db2 = $this->load->database('db2', TRUE);
        }
 
        public function getStudents(){
            $query = $this->db2->get('student');
            return $query->result(); 
        }
 
    }
?>
  • Related