We can create multiple database connections easily by providing second database information in application/config/database.php.
Normally we already set the default database group which is by default.
Like this
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "databasename";
we can create another database connections by creating a new array name as seconddb just exactly a copy of default group array but replace seconddb instead of default in case of second db.
$db['seconddb']['hostname'] = "localhost";
$db['seconddb']['username'] = "root";
$db['seconddb']['password'] = "";
$db['seconddb']['database'] = "seconddatabasename";
Next is we need to send the connection to another variable inside model for second database.
function mytestmodel_method()
{
$otherdb = $this->load->database('otherdb', TRUE);
$query = $otherdb->select('name')->get('person');
var_dump($query);
}
0 Comment(s)