CodeIgniter Forums
Changing db in the middle of a script - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Changing db in the middle of a script (/thread-5711.html)



Changing db in the middle of a script - El Forum - 02-01-2008

[eluser]Skinnpenal[/eluser]
Hi guys Smile

I have CI autoloading the database library, so when the app starts it connects to database specified in my config/database.php

Now, in the middle of this app, I need to connect to a different database .. replacing the current connection.

How do I do this "the right way"? Smile

I tried doing
Code:
$this->load->database($config);
and supplying a new config, but that does absolutely nothing, as far as I can tell.

EDIT:

I got this to work:
Code:
$this->db->close();
$this->db->initialize($config);
Is that the proper way to do it?

And another question:
I use database based sessions (because of freak_auth), which means that when I connect to this new database, freak_auth no longer has access to the session it created in the beginning of the script. Any ideas how to fix that?


Changing db in the middle of a script - El Forum - 02-01-2008

[eluser]Chris Newton[/eluser]
You can connect to multiple databases, without closing one out. See the documentation:

http://ellislab.com/codeigniter/user-guide/database/connecting.html

Bottom of the page.