Jump to content

Best Way To Make Multiple Db Connections


jeger003

Recommended Posts

hello,

I am using three databases. two of them have the same login information. I want to be able to run different queries on the same page from all three databases. So far i am using this but it only uses the last connection no matter which $handle_db i use.

 

 



$handle_db1 = mysql_pconnect("othersite.com","user","pass"); 
mysql_select_db("db1",$handle_db1);
$handle_db2 = mysql_pconnect("mysite.com","user1","pass1"); 
mysql_select_db("db2",$handle_db2);
$handle_db3 = mysql_pconnect("mysite.com","user1","pass1"); 
mysql_select_db("db3",$handle_db3);

 

I tried functions, classes and nothing works.

The two that use the same connection information are actually the same connection and would use the last selected database on that connection, unless you specify a TRUE value for the 4th parameter in the mysql_connect statement.

@socialcloud - i want to keep connections to dbs separate. if you look at my code it is from that php page but it does not work like the user commented.

 

@PFMaBiSmAd - below seems to be working. Could you possibly explain how this works? I'm trying to understand mysql connections. Also what is the best way to handle errors? I want mysql to tell me all the errors rather than blank pages.

 


$handle_db1 = mysql_pconnect("othersite.com","user","pass"); 
mysql_select_db("db1",$handle_db1);
$handle_db2 = mysql_pconnect("mysite.com","user1","pass1"); 
mysql_select_db("db2",$handle_db2);
$handle_db3 = mysql_pconnect("mysite.com","user1","pass1", TRUE); 
mysql_select_db("db3",$handle_db3);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.