The database.php config file
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "123456";
$db['default']['database'] = "database_1";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['second_db']['hostname'] = "localhost";
$db['second_db']['username'] = "root";
$db['second_db']['password'] = "abcdef";
$db['second_db']['database'] = "database_2";
$db['second_db']['dbdriver'] = "mysql";
$db['second_db']['dbprefix'] = "my_";
$db['second_db']['pconnect'] = TRUE;
$db['second_db']['db_debug'] = TRUE;
$db['second_db']['cache_on'] = FALSE;
$db['second_db']['cachedir'] = "";
$db['second_db']['char_set'] = "utf8";
$db['second_db']['dbcollat'] = "utf8_general_ci";
The default group will be loaded automatically during $this->db call. You can use CI legacy_db method to load other db configuration group.
// load second database
$this->legacy_db = $this->load->database(database_2, true);
// fetch result from my_table
$this->legacy_db->select ('*');
$this->legacy_db->from ('my_table');
$query = $this->legacy_db->get();
$result = $query->result ();
$this->legacy_db = $this->load->database(database_2, true);
ReplyDeletedidn't work well for me, but i tried putting the database name on the first parameter
//create a variable
private $legacy_db;
//instantiate the variable with CI db con
$this->legacy_db = $this->load->database('NameOfDbToGetData', true);
// use it
$query =$this->legacy_db->get('some-table');
return $query->result();
$this->legacy_db = $this->load->database(database_2, true);
Deletewill work if you defined a 'database_2' in the config/constants.php with the name of the database group
'$db['Name_of_the_database_group']['hostname']' ....
'$db['Name_of_the_database_group']['username']' ....
'$db['Name_of_the_database_group']['password']' ....