Search code examples
codeignitermultiple-databases

Conneting to more than two databases with codeigniter


I am working on a project which needs to connect with three databases, one in MySQL and the other two in MSSQL. I am using codeigniter as the framework and I can successfully connect to two databases. But when I try to connect to the third MSSQL database using the same method used to connect the second one, all the previous connections are gone. The database config arrays in database.php are correct.

The default configurations are for MySQL

I connected the second db as follows.

$mssql = $this->load->database('mssql1', TRUE);

And the third on as follows

$mssq2 = $this->load->database('mssql2', TRUE);

Thanks in advance


Solution

  • I fixed it by changing the simple_query function in codeigniter DB_driver.php.

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

    added $this->db_select(); to this function. Thanks for all help