I want to auto-load my multiple database so I can use them simultaneously. I have this configuration in my /application/config/database.php
:
$active_group = 'identity';
$query_builder = TRUE;
// DB Connection
$__db['hostname'] = 'localhost';
$__db['username'] = 'root';
$__db['password'] = '1234';
// Database 1
$db['identity'] = array(
'dsn' => '',
'dbdriver' => 'mysqli',
'hostname' => $__db['hostname'],
'username' => $__db['username'],
'password' => $__db['password'],
'database' => 'x1zn2j_identity',
'dbprefix' => 'id_',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'cachedir' => '',
'swap_pre' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
// Database 2
$db['control'] = array(
'dsn' => '',
'dbdriver' => 'mysqli',
'hostname' => $__db['hostname'],
'username' => $__db['username'],
'password' => $__db['password'],
'database' => 'x1eg2x_ctrl',
'dbprefix' => 'ctrl_',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'cachedir' => '',
'swap_pre' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Now to make it more flexible because maybe in the future, I'll add a new database and just include them in my database.php
config file so it will be a hassle-free framework. So what I did was I created a new library called MultiDB.php
and added it to the autoload.php
config file so it will be added on every page load. Then I add this code:
class MultiDB
{
public $dbase = array();
function __construct()
{
global $db;
foreach($db as $database)
{
$this->dbase[$database] = $this->load->database($database, TRUE);
}
}
}
Now obviously, it won't work. I want it to automatically load the DB based on the $db
configuration. So if I'll add a new database configuration, the new database will load and can be used using the Query Builder by CI.
Anyone?