tags:

views:

17

answers:

1

I was just wondering if it is valid to call mysql_pconnect multiple times in one php page? Also, if it is possible do I have to somehow close my last mysql handle or something? Basically is the following code ok? Or lacking some other steps?

mysql_pconnect("ip:3306", "user", "pass") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());

//do some SQL query

mysql_pconnect("ip2:3306", "user", "pass") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());

//do another SQL query
+1  A: 

Yes. But it is probably a better practice to store both handles to both databases and explicitly use the correct db.

 dbServer1 = mysql_pconnect("ip:3306", "user", "pass") or die(mysql_error());
 mysql_select_db("Test", dbServer1) or die(mysql_error());     

 dbServer2 = mysql_pconnect("ip:3306", "user", "pass") or die(mysql_error());
 mysql_select_db("Test", dbServer2) or die(mysql_error());

 // Manipulate both databases
 mysql_query("...", dbServer1);
 mysql_query("...", dbServer2);
Doug T.
I think that's basically what I am asking, does the "hidden" handle automatically get replaced when calling mysql_pconnect the second time.
erotsppa
Yes. http://de.php.net/mysql_query says: "If the link identifier is not specified, _the last link opened by mysql_connect()_ is assumed.". That's true for mysql\_pconnect as well. But I'd urge you to explicitly specify the link identifier, too.
VolkerK