View Full Version : php with 2 databases connection open

02-01-2007, 02:31 PM
I have a program that will be using 2 different databases I have an included file for each to open the db but how does my mysqlquery know which db to query??

02-01-2007, 02:50 PM
Well, how about:

SELECT * FROM db.table

[edit:] Thought I should probably contribute a bit more...

mysql_query() operates on the last initiated link, or the link specified.

So you could also:

$rc1 = mysql_connect(...);
$rc2 = mysql_connect(...);

Depending on your queries...

02-02-2007, 02:01 PM
hmm. the
$rc2 = mysql_connect(...);
will not necessarely create a new link (open a connection). You'll need to set the new_link parameter (the fourth parameter in your mysql_connect(...) to True to force a new link to be opened.

but i don't realy see why you would need two links, unless the db's are on different servers.
else, you can just swith between db's by putting in a mysql_select_db() each time you need another db to become te active one.

$link = mysql_connect(...);
mysql_select_db("db1", $link);
$result = mysql_query("select blablabla", $link);
mysql_select_db("db2", $link);
$result = mysql_query("select blablabla", $link);