Can PHP connect to two MySQL databases simultaneously?

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
s3rg1o
Forum Commoner
Posts: 32
Joined: Sun Feb 16, 2003 4:58 pm

Can PHP connect to two MySQL databases simultaneously?

Post 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?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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?
s3rg1o
Forum Commoner
Posts: 32
Joined: Sun Feb 16, 2003 4:58 pm

Post by s3rg1o »

yes
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

yes to which one?
mgelinas
Forum Newbie
Posts: 13
Joined: Tue Jun 10, 2003 6:33 am
Location: QC Canada

Post 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?
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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. :)
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post 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");
Post Reply