How to connect to 2 database in PHP?

Discussion in 'PHP' started by dynashox, Sep 21, 2008.

  1. #1
    Hi,

    How do I connect 2 mysql database in a single php program? What I want to do is, 1 database will be the main database, while the other one will be a dummy database.

    Seems, the usual way to connect to the database don't work.

    Any help is appreciate.
     
    dynashox, Sep 21, 2008 IP
  2. Codythebest

    Codythebest Notable Member

    Messages:
    5,764
    Likes Received:
    253
    Best Answers:
    0
    Trophy Points:
    275
    #2
    Google is your friend...
     
    Codythebest, Sep 21, 2008 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    What is your usual way?

     
    Colbyt, Sep 21, 2008 IP
  4. dynashox

    dynashox Premium Member Staff

    Messages:
    8,662
    Likes Received:
    563
    Best Answers:
    3
    Trophy Points:
    335
    #4
    Yeah similar to the one that you've shown.

    For database 1 :
    
    
    $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Database ERROR "");
    
    mysql_select_db(DB_NAME,$con) or die ("ERROR: Database not found ");
    
    Code (markup):
    For database 2:

    
    $con1 = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Database ERROR "");
    
    mysql_select_db(DB_NAME1,$con1) or die ("ERROR: Database not found ");
    
    Code (markup):
    But when I call the database, it only connect to the 2nd database and not both.
     
    dynashox, Sep 21, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    i was not allowed to post link =(

    do a search using google : connect to two database sql

    u wil see some topics on this

    good luck
     
    ads2help, Sep 21, 2008 IP
  6. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #6
    Note to anyone reading this thread. I have a typo in my code. There is an extra ".

    As long as all the table names and variable are different I would think that should work.

    EDIT: I did a little research. As I seem to read it you need to call specific tables when connecting to more than one at the same time. The articles for your reading:

    http://www.webmasterworld.com/php/3221902.htm
    http://www.phpbuilder.com/board/archive/index.php/t-10220835.html

    The Google search string that yielded the results
     
    Colbyt, Sep 21, 2008 IP
  7. izwanmad

    izwanmad Banned

    Messages:
    1,064
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    if you not yet solve this problem... here is the solution:

    everytime you're using mysql function .. such as mysql_query() ... always include the connection identifier.. example:

    mysql_query($sql, $con) --- for 1st db
    mysql_query($sql, $con1) --- for 2nd db

    solved!!
     
    izwanmad, Oct 16, 2008 IP