Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Using Multiple Databases

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

like image 625
Pedro Avatar asked Mar 11 '09 12:03

Pedro


People also ask

Can we use multiple database in CodeIgniter?

If your application is built with the CodeIgniter framework, it's exceptionally simple to use multiple databases. CodeIgniter provides an easy way to put through and utilize numerous databases on the same or distinct server.

Can we use multiple databases in laravel & CodeIgniter If yes then how?

If you need to connect to more than one database simultaneously you can do so as follows: $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);

How can I change database in CodeIgniter?

You'll probably need to use sessions to do that. You can create session variable to indicate the current database, and in your controller's constructor, get the variable and load the database.


2 Answers

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); 
like image 137
mrbinky3000 Avatar answered Sep 28 '22 07:09

mrbinky3000


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.

like image 34
Christian Studer Avatar answered Sep 28 '22 06:09

Christian Studer