I'm not sure if you call this the βbestβ way, but a , as described in the tutorial, is,
in the database file, you have a default configuration, part of which:
$db['default']['hostname'] = "localhost"; $db['default']['username'] = "user"; $db['default']['password'] = "database"; $db['default']['database'] = "db1";
now you can create another group, let's say we call it group1, and we want it to have everything the same as the default database settings, except for the name, so you can do
$db['group1']=$db['default']; $db['group1']['database']="db2";
then when you want to use the second database just click
$DB2 = $this->load->database('group1', TRUE);
and then instead of $this->db->foo() you will do $DB2->foo()
alternatively (as suggested in the sbaaaang comments), you can do $this->db=$DB2; so that everything is the same
and you can expand it to several groups, such as
$DB1 = $this->load->database('group1', TRUE); $DB2 = $this->load->database('group2', TRUE); ... $DBn = $this->load->database('groupn', TRUE);
Ahmed-anas
source share