...

View Full Version : mysql_query on 2 db ...



hanpedro
06-20-2005, 05:54 AM
Please help,

I want to have mysql_query on 2 db with same use, password.

code::

<?
$db1[host] = "localhost";
$db1[user] = "abc";
$db1[pass] = "1234";
$db1[db] = "db1";
$db1[dir] = "a/ab";

$db2[host] = "localhost";
$db2[user] = "abc";
$db2[pass] = "1234";
$db2[db] = "db2";
$db2[dir] = "a/cd";

$db[link] = @mysql_connect($db1[host], $db1[user], $db1[pass]) or die("host, user, pass .");
?>
<?
$result_db1 = mysql_result(mysql_query("SELECT no,name FROM db1.control_table WHERE 1 LIMIT 1"), 0, 0); // databaseA.tableA.fieldA SELECT!
while($data=mysql_fetch_array($result_db1)) {
?>
<?
$result_db2 = mysql_result(mysql_query("SELECT no,name FROM db2.control_table WHERE 1 LIMIT 1"), 0, 0); // databaseA.tableA.fieldA SELECT!while($data=mysql_fetch_array($result_db2)) {
?>

Any comment would be appreciated.

delinear
06-20-2005, 09:52 AM
There's no problem with using two separate DB connections in one script, but remember that PHP will, by default, always use the last open connection unless you specify otherwise. Therefore the $db2 query would use the $db1 connection unless you specifically tell it to use a different connection. You can do this using an argument in the mysql_query function:


$db1[link] = @mysql_connect($db1[host], $db1[user], $db1[pass]) or die("host, user, pass .");
$db2[link] = @mysql_connect($db2[host], $db2[user], $db2[pass]) or die("host, user, pass .");

$result_db1 = mysql_result(mysql_query("SELECT no,name FROM db1.control_table WHERE 1 LIMIT 1", $db1[link]), 0, 0); // databaseA.tableA.fieldA SELECT!
while($data=mysql_fetch_array($result_db1)) {
// do something
}

$result_db2 = mysql_result(mysql_query("SELECT no,name FROM db2.control_table WHERE 1 LIMIT 1", $db2[link]), 0, 0); // databaseA.tableA.fieldA SELECT!
while($data=mysql_fetch_array($result_db2)) {
// do something
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum