phpcodeigniteractiverecordmultiple-databases

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.


Solution

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