Quote:
Originally Posted by karimali831
Is this possible? I want to connect to two databases from one php page. Both databases are on the same machine also. I use a CMS and want it to read a group of tables from db1, and another group of tables frmo db2.
any help much appreciated
thank you
|
Hi Karim,
If you are trying to open multiple, separate MySQL connections with the same MySQL user, password, & hostname/IP, you must set
$new_link = TRUE to prevent mysql_connect from using an existing connection. This parameter has been available since PHP 4.2.0 and allows you to open a new link even if the call uses the same parameters.
For example, you are opening two separate connections to two different databases (but on the same host, & with the same user & password):
PHP Code:
$db1 = mysql_connect($dbhost, $dbuser, $dbpass);
$rv = mysql_select_db($dbname1, $db1);
$db2 = mysql_connect($dbhost, $dbuser, $dbpass);
$rv = mysql_select_db($dbname2, $db2);
At this point, both $db1 & $db2 will have selected the database named by $dbname2.
The workaround is to require that the second MySQL connection is new:
PHP Code:
$db1 = mysql_connect($dbhost, $dbuser, $dbpass);
$rv = mysql_select_db($dbname1, $db1);
$db2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE);
$rv = mysql_select_db($dbname2, $db2);
Now, $db1 should have selected $dbname1, & $db2 should have selected $dbname2.
ie: by calling
mysql_select_db($database) before every SQL query just to be sure which database you are working with.
Note: This occurs only when the server, username, & password parameters are identical for each mysql_connect statement..