32

database.php:

$db['default']['hostname'] = "192.168.2.104";
$db['default']['username'] = "webuser";
$db['default']['password'] = "----";
$db['default']['database'] = "vad";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['stats']['hostname'] = "192.168.2.104";
$db['stats']['username'] = "webuser";
$db['stats']['password'] = "---";
$db['stats']['database'] = "vad_stats";
$db['stats']['dbdriver'] = "mysql";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";

The issue is I can only define in the configuration one $active_group, default, or stats. I followed the CodeIgniter documentation and I added the following:

$DB2 = $this->load->database('stats', TRUE);

This way I connect to the second database, but I lose the connection to the first one. Does anyone have any ideas on how can I load the two database without having to do the following in all models constructors?

$database1 = $this->load->database('database1', TRUE);
$database2 = $this->load->database('database2', TRUE); 

Regards,

Pedro

6 Answers 6

29

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);
4
  • CodeIgniter still exhibits this inexcusable bug in Oct 2013, and this fix does not work. Commented Oct 25, 2013 at 20:35
  • If you are starting a new project, might I suggest using FuelPHP. A lot of the same people who wrote CodeIgniter migrated to this complete re-write and re-think of CodeIgniter. If you are on a legacy project, or your client requires CodeIgniter, this fix should work. But if you find another way to fix it, please share here. Thanks! Commented Oct 29, 2013 at 19:13
  • This is still applicable for CI (version 2.1.3) and much better than manually calling db_select() every time.
    – xd6_
    Commented Mar 3, 2014 at 22:07
  • $this->db_select(); so needs to put db name in this selector right ? Commented Jul 27, 2017 at 16:27
24

Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.

0
5

currently, codeigniter cannot connect to multiple database in persisten connection. so, you should turn of the persisten of your connections. you may can do this..

$db['default']['pconnect'] = FALSE;

$db['stats']['pconnect'] = FALSE;
1
  • so we can mention database name in db_select funtion ? Commented Jul 27, 2017 at 16:26
4

I fix the problem changing the DB_driver.php on the framework.

In this function I add $this->db_select(); and you never lose your connection again when working with 2 databases.

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

    $this->db_select();

    return $this->_execute($sql);
}
3

You may try modifying function CI_Session() in session.php file.

Replace

$this->CI->load->database();

with this

$this->CI->db1 = $this->CI->load->database('default', TRUE);
$this->CI->db2 = $this->CI->load->database('db2', TRUE);

In this way, you need not load 2 dbs in all the model files but shall use them directly using objects.

$this->db1 would be accessing default group db and $this->db2 would be accessing db2 group db. (both db groups should have been defined in database.php)


Sundar

1

You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$this->db->db_select($database2_name);

CodeIgbiter User Guide

Not the answer you're looking for? Browse other questions tagged or ask your own question.