I have codeigniter setup to connect to 2 databases (default and site). I need to change the default database name dynamically and anything that uses the default connection should also use the new database. Is there a way to do this?
The reason for this is I want to setup a cron script that runs commands on certain databases. I need to be able to dynamically change this and editing application/config/database.php is not possible.
$active_group = 'default';
$active_record = TRUE;
$phppos_client_name = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));
$db['site']['hostname'] = 'php-pos-db';
$db['site']['username'] = 'phppoint';
$db['site']['password'] = 'password';
$db['site']['database'] = 'phppoint_site';
$db['site']['dbdriver'] = 'mysql';
$db['site']['dbprefix'] = '';
$db['site']['pconnect'] = FALSE;
$db['site']['db_debug'] = FALSE;
$db['site']['cache_on'] = FALSE;
$db['site']['cachedir'] = '';
$db['site']['char_set'] = 'utf8';
$db['site']['dbcollat'] = 'utf8_general_ci';
$db['site']['swap_pre'] = '';
$db['site']['autoinit'] = TRUE;
$db['site']['stricton'] = FALSE;
$db['default']['hostname'] = "php-pos-db";
$db['default']['username'] = "phppoint";
$db['default']['password'] = "password";
$db['default']['database'] = "phppoint_$phppos_client_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "phppos_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_unicode_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
/* End of file database.php */
/* Location: ./application/config/database.php */