You should change the settings for accessing your main database in your application/configdatabase.php file to:
$config['default']['hostname'] = "localhost";
$config['default']['username'] = "myusername";
$config['default']['password'] = "mypassword";
$config['default']['database'] = "mydatabase";
$config['default']['dbdriver'] = "mysql";
$config['default']['dbprefix'] = "";
$config['default']['pconnect'] = FALSE;
$config['default']['db_debug'] = TRUE;
$config['default']['cache_on'] = FALSE;
$config['default']['cachedir'] = "";
$config['default']['char_set'] = "utf8";
$config['default']['dbcollat'] = "utf8_general_ci";
Then a few lines above, in the same file, set the main database group:
$active_group = 'default';
Still in the same file, for every other database you need to connect to, create a new set of settings, changing the group identification and connection settings:
$config['secundario']['hostname'] = "localhost";
$config['secundario']['username'] = "myusername";
$config['secundario']['password'] = "mypassword";
$config['secundario']['database'] = "mydatabase";
$config['secundario']['dbdriver'] = "mysql";
$config['secundario']['dbprefix'] = "";
$config['secundario']['pconnect'] = FALSE;
$config['secundario']['db_debug'] = TRUE;
$config['secundario']['cache_on'] = FALSE;
$config['secundario']['cachedir'] = "";
$config['secundario']['char_set'] = "utf8";
$config['secundario']['dbcollat'] = "utf8_general_ci";
In your methods when calling $this->db you will be acting in the main database. Whenever you want to act on another database, simply load it dynamically, as follows:
$bd_secundario = $this->load->database('secundario', TRUE);
After charging, you can operate on it in the same way as on $this->db
, as an example,
$bd_secundario->query('SELECT * FROM world').
Supposing array of configurations be [so],(http://pastebin.com/fmCra9vy) just load the information of this other DB in this way:
$DB2 = $this->load->database('outrodb', TRUE);
That would probably solve.– stderr
I also saw this possibility, but there is a problem, the hostname of this other connection is stored in a session, so I can’t recover the value of this session in the database.php file
– Meeeefiu