Multiple DB connection - System get the wrong one

#1
[eluser]cinewbie81[/eluser]
Hi all,

I have 2 database connections as following:
Code:
$dbClient = $this->load->database('client', TRUE);
$dbServer = $this->load->database('server', TRUE);

I want to make sure if the system get the correct db connection or not, therefore here is my verification code:

$dbClient->query('show tables');
$dbServer->query('show tables')

both $dbClient and $dbServer will return me 'server' database tables ..
And when i load server database 1st then only client as following:
Code:
$dbServer = $this->load->database('server', TRUE);
$dbClient = $this->load->database('client', TRUE);

Now both $dbClient and $dbServer will return me 'client' database table ..

It seems to me that the system will get the last database it loaded as the connection. Any idea why ? Hope someone can help, thanks.

#2
[eluser]cinewbie81[/eluser]
Anyone ? at least tell me where i went wrong in the code as im stuck here for 2 days already Sad

#3
[eluser]xwero[/eluser]
i believe you have to set the
Code:
$config['pconnect'] = FALSE;
to true in the config/database.php file

#4
[eluser]cinewbie81[/eluser]
It's already SET to True , but it doesn't working still

the following is by config/database.php

Code:
$active_group = "client";
$db['client1']['hostname'] = "localhost";
$db['client1']['username'] = "root";
$db['client1']['password'] = "password";
$db['client1']['database'] = "client1";
$db['client1']['dbdriver'] = "mysql";
$db['client1']['dbprefix'] = "";
$db['client1']['active_r'] = TRUE;
$db['client1']['pconnect'] = TRUE;
$db['client1']['db_debug'] = TRUE;
$db['client1']['cache_on'] = FALSE;
$db['client1']['cachedir'] = "";


$active_group = "server";
$db['server']['hostname'] = "localhost";
$db['server']['username'] = "root";
$db['server']['password'] = "myserverdb";
$db['server']['dbdriver'] = "mysql";
$db['server']['dbprefix'] = "";
$db['server']['active_r'] = TRUE;
$db['server']['pconnect'] = TRUE;
$db['server']['db_debug'] = TRUE;
$db['server']['cache_on'] = FALSE;
$db['server']['cachedir'] = "";

#5
[eluser]xwero[/eluser]
remove one active group i think that is the problem.

#6
[eluser]cinewbie81[/eluser]
hi,

how do u mean with remove one active group ?
I need 2 database connection, one is 'server', another one is 'client' ..
that's why i created 2 active group as above..

If let's say i remove 1 active group(let's say client), I'll have only server connection, then how am i going to connect to client database ? OR i misunderstood u ?

#7
[eluser]xwero[/eluser]
I'm not totally sure but the active group is used by the database library to identify the right database when you load the database without arguments. But because you are setting the database objects with the second argument they are both loaded.

I think because you have two active_groups defined the database class shows strange behavior.

#8
[eluser]cinewbie81[/eluser]
maybe then ..
but then i still don't know what's the solution for it Sad

#9
[eluser]xwero[/eluser]
Have you tried removing one active group? I don't think it matters which one you remove because the two databases are loaded into separate objects using the second argument

#10
[eluser]cinewbie81[/eluser]
u mean remove one of the active_group in database config file ?
now my database config file will looks like this:
Code:
$active_group = "client";
$db['client1']['hostname'] = "localhost";
$db['client1']['username'] = "root";
$db['client1']['password'] = "password";
$db['client1']['database'] = "client1";
$db['client1']['dbdriver'] = "mysql";
$db['client1']['dbprefix'] = "";
$db['client1']['active_r'] = TRUE;
$db['client1']['pconnect'] = TRUE;
$db['client1']['db_debug'] = TRUE;
$db['client1']['cache_on'] = FALSE;
$db['client1']['cachedir'] = ""

and my code

$dbClient = $this->load->database('client', TRUE);

will works with $dbClient only ..
if i want to save database in my server database then how ?
Do i have to do something like $dbClient->query('use database server'), then only save it to server database ? Means i have to manually call the query 'use which database' then only save it to corresponding db ?>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.