3

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.

1

2 Answers 2

9

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';
//..........
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

the other one is initialized in the model.. this one can be used in library, if you want to make one :) jsut another way on how to use.. notice the $this->CI it means im getting the instance of CI so that I can use it as helper or library..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.