简体   繁体   English

Codeigniter多个数据库连接

[英]Codeigniter Multiple Database connections

I'm diving into multiple database usage. 我正在深入研究多种数据库用法。 According to the codeigniter user guide. 根据codeigniter用户指南。 To connect to the additional databases use use the following 要连接到其他数据库,请使用以下命令

$db2 = $this->load->database('second');

then to interact use, 然后互动使用,

$db2->get('second_table');

I'm receiving a Fatal error call to a member function "where()" on a non-object. 我在非对象上收到成员函数“where()”的致命错误调用。

for the following line 对于以下行

$db2->where('field1', $data['item']);

and also for 还有

$db2->get('second_table');

Where am I going wrong with this? 我哪里错了?

Thanks for any help. 谢谢你的帮助。

In order to return the database object, you need to pass a TRUE as second paramenter: 要返回数据库对象,需要传递TRUE作为第二个参数:

$db2 = $this->load->database('second', TRUE);

See the manual on database class for more info. 有关详细信息,请参阅数据库类手册

Make also sure you've loaded the config for that database in application/config/database.php 还要确保已在application / config / database.php中加载该数据库的配置

$db['default']['hostname'] = 'localhost';
//.........

$db['second']['hostname'] = 'localhost';
//..........

In config/database.php 在config / database.php中

/ /

* DB1 */
$active_group = "forum";
$active_record = TRUE;

$db['DB1']['hostname'] = "xxxxx";
$db['DB1']['username'] = "xxxxx";
$db['DB1']['password'] = "xxxxx";
$db['DB1']['database'] = "xxxxx";
and other configs....

/* DB2 */

$db['DB2']['hostname'] = "xxxxx";
$db['DB2']['username'] = "xxxxx";
$db['DB2']['password'] = "xxxxx";
$db['DB2']['database'] = "xxxxx";
$db['DB2']['dbdriver'] = "mysql";
$db['DB2']['dbprefix'] = "";
and so on...

you can use databases by 你可以使用数据库

$this->DB1 = $this->CI->load->database('DB1', TRUE);
$this->DB2 = $this->CI->load->database('DB2', TRUE);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM