CodeIgnite即时更改默认数据库名称 [英] CodeIgnite change default database name on the fly
问题描述
我有codeigniter设置,可以连接2个数据库(默认数据库和站点数据库)。我需要动态更改默认数据库名称,并且使用默认连接的任何内容也应使用新数据库。有办法吗?
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?
原因是我想设置一个在特定数据库上运行命令的cron脚本。我需要能够动态更改它,并且无法编辑application / config / database.php。
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 */
推荐答案
您想要的方式我认为这是不可能的。您可以选择其他方式。
The way you want I think its not possible. you can do it alternative way.
1。请勿在自动加载配置中加载数据库。
2.使用 $ this->手动加载数据库; load-> database('site')
或 $ this-> load-> database('default')
1.Do not load database at your autoload config.
2.load your database manually using $this->load->database('site')
or $this->load->database('default')
before executing query.
这篇关于CodeIgnite即时更改默认数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!