CodeIgniter Forums
Active Record & Multiple Databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Active Record & Multiple Databases (/thread-24895.html)



Active Record & Multiple Databases - El Forum - 11-23-2009

[eluser]Spicer[/eluser]
So I understand you can connect to more then one database doing something like this.
Code:
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

My problem is I have written a pretty big application using active record and one (default) database connection. So all my quires look like this.

Code:
$this->db->query();
$this->db->result();

It has come to my attention I need to move half of the database to another database. So I need two connections. So my quires should look something like this.

Code:
$DB1->query();
$DB1->result();
$DB2->query();
$DB2->result();

Now, I know I can do a search and replace and I will if I must, but is there a way to keep the current 'default' database be $this->db and add a second or third connection. So something like this.

Code:
$this->db->query();
$this->db->result();
$data1 = $this->db->result();

$DB->qurey();
$DB->result();
$data2 = $DB->result();

Every time I load the second database connection and assign it to $DB I lose the connection on $this->db.

Sorry this might be a dumb question but the inter-workings of the database class is something I do not understand all that well in CI.


Active Record & Multiple Databases - El Forum - 11-23-2009

[eluser]CI Coder[/eluser]
Try setting ['pconnect'] = FALSE in your config array for all of them. You will lose the connection to the first database because mysql_pconnect will look for an open connection to the same host with the same user/password and return that instead of opening a new connection. So, if all your databases are on localhost or in general on the same host, you will have that as long as you use pconnect.

I've only had this problem once and that was a while ago. I hope it helps.

Good luck!


Active Record & Multiple Databases - El Forum - 12-05-2009

[eluser]Spicer[/eluser]
Thanks a bunch CI Coder!!! That solved the problem!!!