Switching the default link resource for mysql_query()?

Discussion in 'PHP' started by Kadence, Jan 2, 2008.

  1. #1
    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?
     
    Kadence, Jan 2, 2008 IP
  2. goliathus

    goliathus Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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...
     
    goliathus, Jan 2, 2008 IP
  3. Kadence

    Kadence Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #3
    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.
    Actually the most recently opened connection becomes the default. Last in first out I guess :)
     
    Kadence, Jan 4, 2008 IP