简体   繁体   中英

Codeigniter Multiple Database connections

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.

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';
//..........

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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