Welcome Guest, Not a member yet? Register   Sign In
Using multiple MySQL databases
#1

[eluser]stevefink[/eluser]
Hi all,

First and foremost, I'm having a blast with the CI learning process! Unfortunately I've run into a hurdle where I'm going to have to ask the community for some insight. I'm working on a small application that requires speaking to two separate databases. I followed the advice listed here in order to achieve this:

http://ellislab.com/codeigniter/user-gui...cting.html

So in config/database.php I've created two elements in the $db array consistent of $db['db1'] and $db['db2']. My questions are the following:

1) Do we leave $active_group defined if we're using multiple databases?
2) Where would be the best place to define the actual DB object instances? The constructor or the index() method?

I wrote the following code in my controller's index():

function index()
{
$DB1 = $this->load->database('db1', TRUE);
$DB2 = $this->load->database('db2', TRUE);

// retrieve all records.
$data['query'] = $DB1->get('tbl_signups');

.. yadda yadda ..
This query is unfortunately is trying to query the database defined in $DB2 (probably cause of a user error. Tongue )

Thanks again for any insight folks!
#2

[eluser]Seppo[/eluser]
are you using both persistent connections?
I ask this, 'cause
mysql_pconnect
Quote:First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
#3

[eluser]stevefink[/eluser]
Ah yes, great call, Seppo!

Disabling pconnect sure did the trick.

BTW, is it considered good practice to assign the objects in the index() function or better directly in the constructor?

Thanks again!
#4

[eluser]Martin Penev[/eluser]
depens on how much you use the objects. if just once, inside the desired function (index or whatever) is fine. Should you use the objects more often, the constructor is a good thing




Theme © iAndrew 2016 - Forum software by © MyBB