1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to connect to two databases simultaneously from 2 different server?

Discussion in 'PHP' started by Stringx, Feb 16, 2011.

  1. Stringx

    Stringx New Member

    Joined:
    Feb 9, 2011
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0
    Hi,

    How to connect to two databases simultaneously from 2 different server?

    E.g.
    I have established a connection to db1, while on 'while loop' a certain field needs to access data from db2 from different server. How this would be implemented, using MySQL and PHP?

    Stringx
     
  2. underground_devil

    underground_devil New Member

    Joined:
    Jan 24, 2011
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    PHP developer ,trainer
    Location:
    Punjab
    i never tried but i am sure u can do by using two connection files:)
     
  3. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    its easy i think
    you should make two db handlers

    PHP:
    //your primary connection
    $conn mysql_connect("host1","username","password");

    if(
    $conn){
       
    //connect to your first db with first handle
       
    mysql_select_db('database1'$conn); 

       
    $conn2 mysql_connect("host1","username","password",true); 
        
    //should put that true to make a new connection

       //connect to your second db with 2nd handle
       
    mysql_select_db('database2'$conn2); 
    {
    you can make with the same handle as well

    and dont use the MAKE connection thing inside a loop
    unless it will make multiple connections
    if you have no option, use mysql_pconenct
    but if you put your code, i can tell you better :D
     
  4. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    if your trying to query one database using fields from another you could but you'd need to know the fields before hand. Return the first database result as an associative array and us that in your second query. If you return both as the same array it should add the data together.
    PHP:
    <?php
    $C1 
    = @mysql_connect('localhost','pein87','password');
    $C2 = @mysql_connect('localhost:81','pein87','password');

    $DB1 = @mysql_select_db('boo',$C1);
    $DB2 =@mysql_select_db('boo'$C2);

    $Q1 = @mysql_query("SELECT * FROM ghosts");

    $ghostData = @mysql_fetch_assoc($Q1);

    $Q2 = @mysql_query("SELECT * FROM ghostLocation WHERE '"$ghostData[HauntingType] ."' = 'demonic'");

    $ghostData[] = @mysql_fetch_assoc($Q2);

    echo 
    $ghostData;

    ?>
    This should work but of course you'll need to tweak your SQl statements though. What it will do is add more indexes to the $ghostData array. Knowing the fields of each table being called is essential for this to work though. I don't use array_merge() because of the overhead of calling a function when using $array_name[] = $Blah works better and faster. I hope this works out for you and as a rule of thumbs you'll need both connections because the mysql_select_db() will use the current active connection if you don't tell it which one to use.
     
  5. etiergjt

    etiergjt New Member

    Joined:
    Mar 21, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    yes,it's not hard,I figured it out when I try to integrate wiki with phpbb,there is a ready-made wiki plugin do that to verify phpbb users
     

Share This Page