Possible Duplicate:
How do you connect to multiple MySQL databases on a single webpage?
If I want to connect to one db do some query, and then later do another query from another DB. How do I do it? Do I just
mysql_pconnect("host:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
//do some query
mysql_pconnect("host2:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
//do another query
Is that how you do it? A couple of questions. Notice I used pconnect, does that affect calling it twice on the same page? Also, do I have to close the connection for the first one before calling the second one?
You need to store database connection link in separate variable. For example
This will leave your script with two open connections to different hosts until it ends.
You may reuse either of these connections by calling
mysql_pconnect
again.From the docs:
Since your hosts are different, there will be two different connections
You cannot explicitly close a connection open with
mysql_pconnect
.You did RTM, right, because you're not using the
$link_identifier
?http://us.php.net/mysql_select_db :
Whenever you have use the pconnect, you're sharing the connection (not only within the page, but possibly with other pages) -- in your case here, don't do that. You want isolated links, therefore isolated transactions. You should probably consider
mysql_connect
instead, and explicitly using thenew_link
parameter. Lastly, use the$link_identifier
explicitly, so you are clear what you are connecting to.You need to store the resource returned from mysql_connect and use it when doing mysql_select_db.
Then use $res1 or $res2 when querying the corresponding db.