Codeigniter - 使用多个数据库 [英] Codeigniter - Using Multiple Databases

查看:103
本文介绍了Codeigniter - 使用多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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); 

尊敬的,

Pedro

推荐答案

而不是应用Camacho提到的hack,你也可以将database.php文件中的pconnect标志设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆