Page 1 of 1

2 times mysql_connect

Posted: Sat Mar 08, 2003 1:20 pm
by wizzard
Hi,

is it possible to connect to 2 different databases in the same code i have 2 different tables on different servers and i want to connect to them in the same php file. like

mysql_connect
code....
mysql_connect ...

?

Posted: Sat Mar 08, 2003 1:54 pm
by Stoker
You can have as many as you like, but you can not make a single query that spans databases on different servers.. also keep in mind that you never need more than onbe connection to one database even if running multiple queries and working with the results at the same time

2 different databases, 4 query resources:

$db1 = mysql_connect('server','user','password');
$db2 = mysql_connect('server2','user','password');

mysql_select_db('dbname',$db1);
mysql_select_db('somedbname',$db2);

$res1db1 = mysql_query('SELECT col1,col2 FROM table1',$db1);
$res2db1 = mysql_query('SELECT col1,col2 FROM table2',$db1);
$res1db2 = mysql_query('SELECT col1,col2 FROM sometable',$db2);
$res2db2 = mysql_query('SELECT col1,col2 FROM beertable',$db2);

while ($row = mysql_fetch_assoc($res2db1)) {
$otherrow = mysql_fetch_assoc($res1db2));
# do soemthing and stuff
}

Posted: Sat Mar 08, 2003 2:28 pm
by wizzard
thanks.