Codeigniter 多个数据库连接

发布于 2024-12-10 17:43:54 字数 426 浏览 0 评论 0原文

我正在深入研究多种数据库的使用。根据 codeigniter 用户指南。要连接到其他数据库,请使用以下命令

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

进行交互使用,

$db2->get('second_table');

我收到对非对象上的成员函数“where()”的致命错误调用。

对于以下行

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

以及

$db2->get('second_table');

我在哪里出错了?

感谢您的任何帮助。

I'm diving into multiple database usage. According to the codeigniter user guide. 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2024-12-17 17:43:54

为了返回数据库对象,您需要传递 TRUE 作为第二个参数:

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

请参阅手册 有关数据库类的更多信息。

还要确保您已在 application/config/database.php 中加载该数据库的配置

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

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

In order to return the database object, you need to pass a TRUE as second paramenter:

$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

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

$db['second']['hostname'] = 'localhost';
//..........
所有深爱都是秘密 2024-12-17 17:43:54

在 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...

你可以通过以下方式使用数据库

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

In 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);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文