Home and Family Articles Directory - Loans - Bad Credit Loan - Mortgages - Loans

PDA

View Full Version : Switching the default link resource for mysql_query()?


Kadence
Jan 2nd 2008, 5:22 am
Say I have connections to two databases on different servers, $dbh1 and $dbh2. How do I switch the 'current', default connection that mysql_query() uses?

For instance if I do mysql_select_db($dbname, $dbh2), this doesn't seem to make $dbh2 the current connection - I have to explicitly use mysql_query('...', $dbh2) to use the $dbh2 handle. mysql_query('...') without a link identifier seems to assume $dbh1 by default, if $dbh1 was opened more recently. Is there a way to make $dbh2 the default connection without reopening it?

goliathus
Jan 2nd 2008, 8:08 am
I think you have to put connections into any function and call it where you need.

Regarding your problem I think $dbh1 was first connection, so it don't connect to $dbh2... first in, first out :)

if you will call your own connect function and also close function (or only mysql_close();) , you can always work with DB which you really need...

Kadence
Jan 4th 2008, 12:57 pm
Yes, I know you can specify the connection in mysql_query(), which I've learned is a good practice when dealing with multiple connections. But I was wondering if there was any way to change the default connection back and forth, like mysql_select_db() does for databases, because I have a lot of code that would need editing if that's not possible.

I guess using mysql_close() and then reopening the connection later would be one option, as you said.
Regarding your problem I think $dbh1 was first connection, so it don't connect to $dbh2... first in, first out :)
Actually the most recently opened connection becomes the default. Last in first out I guess :)