Codeigniter – Using Multiple Databases

There is a bug in codeigniter. Inserting one line into a class will fix the whole thing. Here is the original source: http://koorb.wordpress.com/2007/11/16/codeigniter-connect-to-multiple-databases/

** This fix does not apply to PostgreSQL

Here is a copy just in case that site goes down.

The line number has changed. Here is the bug fix from codeigniter:

start bugfix

Description

all of the database calls go to the same database (last one initialized)

To fix the problem change the simple_query function in /system/database/DB_driver.php:

function simple_query($sql)
{
    if ( ! $this->conn_id)
    {
        $this->initialize();
    }
   
    $this->db_select(); //<-----------------  Added this line
    return $this->_execute($sql);
}

This completely fixes the problem, so you can do stuff like this in a model

$this->legacy_db = $this->load->database('legacy', true);

Leave a Comment