Page 1 of 1

Can PHP connect to two MySQL databases simultaneously?

Posted: Mon Jun 16, 2003 5:51 pm
by s3rg1o
I am working with a script and I need to pull data from two different databases at the same time which I don't think I have ever done. Now I am noticing a problem when I try to access elements from the two databases. Can PHP access two MySQL databases at the same time?

Posted: Mon Jun 16, 2003 6:08 pm
by Galahad
Do you mean on two different servers? same server two different databases (something like 'use database_name')? different tables in the same database?

Posted: Mon Jun 16, 2003 6:14 pm
by s3rg1o
yes

Posted: Mon Jun 16, 2003 6:14 pm
by trollll
I think you can only access two mysql databases (on the same server) simultaneously if you use two different mysql_db_query($db,$query,$link)'s. It makes joins across databases a bit more difficult, but with some creative php-ing I think you could pull it off.

Posted: Mon Jun 16, 2003 6:25 pm
by Galahad
yes to which one?

Posted: Mon Jun 16, 2003 10:23 pm
by mgelinas
I would like to be specific. Can we have two simultaneous MySQL connections to connect to two databases on the same server (in the same user account space, same host, same password)? I tried that yesterday but got into trouble.

I have a custom session handler and it looks like the session_close function closes both connections.

If we can have two simultaneous connections as described, how can this be done? Is there a special trick?

Posted: Mon Jun 16, 2003 11:15 pm
by trollll
Based on your last posting, it should work under the right circumstances. I got it to work using the following structure:

Code: Select all

$link1 = mysql_connect("$server", "$username", "$password") or die("nope");
mysql_select_db($database_name1) or die("nope");
    
$link2 = mysql_connect("$server", "$username", "$password") or die("nope");
mysql_select_db($database_name2) or die("nope");

$result1 = mysql_query("SELECT COUNT(*) FROM $table_name1") or die("nope");
$result2 = mysql_query("SELECT COUNT(*) FROM $table_name2") or die("nope");
    
mysql_free_result($result2);
mysql_close($link2);
        
mysql_free_result($result1);
mysql_close($link1);
Obviously I spared you looking at printing the output and whatnot (which I did just make absolutely sure that it worked), but you get the idea.

If you check out the php manual's mysql section it has some brief info on the configuration options, such as the maximum number of connections allowed. This may have something to do with it, but I doubt it. The next bit gave me a slight shock.

When I specified a link resource for the query to use (even if I used mysql_db_query) it refused to run a single query. It only ran perfectly when I let it figure it out for itself.

Go figure. :)

Posted: Tue Jun 17, 2003 2:25 am
by []InTeR[]
resource mysql_query ( string query [, resource link_identifier])

The link_identifier is the resource of mysql_connect

In the code above, the query's will be executed @ one database, if you supply the link_indentifier to the mysql_query, everything works.

Code: Select all

$result1 = mysql_query("SELECT COUNT(*) FROM $table_name1",$link1) or die("nope");
$result2 = mysql_query("SELECT COUNT(*) FROM $table_name2",$link2) or die("nope");