connecting to two mysql databases...

Discussion in 'Web Design & Coding' started by siLk, Feb 16, 2004.

  1. siLk

    siLk OSNN One Post Wonder

    Messages:
    6
    ...possible with php?
     
  2. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Yep.
    Code:
    $connection_one = mysql_connect("server1","user","pass");
    $connection_two = mysql_connect("server2","user,"pass");
    
    mysql_select_db("db1",$connection_one);
    mysql_select_db("db2",$connection_two);
    
    mysql_query("SELECT * FROM bla1",$connection_one);
    mysql_query("SELECT * FROM bla2",$connection_two);
    
    mysql_close($connection_one);
    mysql_close($connection_two);
    This should work. Theoratically that is, because I have never tried it.
     
  3. Howling Wolf

    Howling Wolf We did not deserve this !

    Messages:
    1,243
    Location:
    Amongst the Native Ones
    Should work fine as it seems so logical ! ;)
     
  4. siLk

    siLk OSNN One Post Wonder

    Messages:
    6
    ok I'll give it a shot, I am pretty sure I have tried this in the past, but I might have done something wrong then ;)

    I'll edit this post if it works or not...
     
  5. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    *waits* I am curious myself, I might need it too in the future.
     
  6. siLk

    siLk OSNN One Post Wonder

    Messages:
    6
    yep it works. here is the connect code I used with passwords etc removed ;)

    PHP:
    $conn mysql_connect("localhost""username""password");
    mysql_select_db("database_1_name"$conn);
    mysql_select_db("database_2_name"$conn);
    you can see this in effect at http://www.wcreplays.com/db_test.php the top 2 rows of usernames were pulled from a seperate database that I use for the staff backend, and the huge list below it is the list of forum usernames
     
  7. siLk

    siLk OSNN One Post Wonder

    Messages:
    6
    sorry took so long to reply.

    anyway, you can do it by connecting to the database you want to execute the query on before the query is executed. That's the only way ive been able to get it to work correctly

    $db_one_connection
    $query_one

    $db_two_connection
    $query_two

    etc
     
  8. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    mysql_db_select, it allows you to specify a DB connection to use.
     
  9. ezfilehosting.com

    ezfilehosting.com OSNN One Post Wonder

    Messages:
    3
    Re: connecting to 2 mysql databases...

    I have a question can these 2 database be join together? www.ezfilehosting.com/forum

    and www.ezfilehosting.com/a I would like to know if I cna connect and how can I connect them? I try wat you guys posted above and I cannot get it to work :(