2 times mysql_connect

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

2 times mysql_connect

Post 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 ...

?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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
}
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

thanks.
Post Reply