Codeigniter - 使用多个数据库 [英] Codeigniter - Using Multiple Databases
问题描述
database.php:
$db['default']['hostname'] = "192.168.2.104";
$db['default']['username'] = "webuser";
$db['default']['password'] = "----";
$db['default']['database'] = "vad";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['stats']['hostname'] = "192.168.2.104";
$db['stats']['username'] = "webuser";
$db['stats']['password'] = "---";
$db['stats']['database'] = "vad_stats";
$db['stats']['dbdriver'] = "mysql";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";
问题是我只能在配置中定义一个$active_group
、默认值或统计信息.我遵循了 CodeIgniter 文档并添加了以下内容:
The issue is I can only define in the configuration one
$active_group
, default, or stats. I followed the CodeIgniter documentation and I added the following:
$DB2 = $this->load->database('stats', TRUE);
通过这种方式我连接到第二个数据库,但与第一个数据库的连接丢失了.有没有人对如何加载两个数据库而不必在所有模型构造函数中执行以下操作有任何想法?
This way I connect to the second database, but I lose the connection to the first one. Does anyone have any ideas on how can I load the two database without having to do the following in all models constructors?
$database1 = $this->load->database('database1', TRUE);
$database2 = $this->load->database('database2', TRUE);
问候,
佩德罗
推荐答案
除了应用 Camacho 提到的 hack 之外,您还可以将 database.php 文件中的 'pconnect'-flag 设置为 FALSE,用于所有连接.
Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.
这篇关于Codeigniter - 使用多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!