Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    91
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Post mysql_query on 2 db ...

    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.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $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]), 00); // 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]), 00); // databaseA.tableA.fieldA SELECT!
    while($data=mysql_fetch_array($result_db2)) {
        
    // do something



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •