Problem connecting multiple Codeigniter databases

Asked

Viewed 1,201 times

5

I have an application where it connects to multiple databases, initially I use the file database settings database php..

Only when I connect to another bank, it still loads the file settings database php., and I do the following to make the second connection:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);
  • 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.

  • 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

1 answer

5

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').

Browser other questions tagged

You are not signed in. Login or sign up in order to post.