how to connect to two database

Discussion in 'PHP' started by moheballah, Aug 29, 2009.

  1. #1
    hi,

    i have script connect to two database and take data from first to input it into second database after making some changes

    this script using mysql_connect two times with two socket
    $DB1
    $DB2

    when i want to execute query in first i using
    mysql_query($query, $DB1);

    and second
    mysql_query($query, $DB2);


    it's working fine in my server and many others server
    but with some people they told me its not works with them
    when i tested it in there servers i noticed that when i open connection with the second database i lost connection with the first

    i think there are settings i should make it in my.conf

    any help ?
     
    moheballah, Aug 29, 2009 IP
  2. The PHP Guy

    The PHP Guy Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change the value of "mysql.max_links" in your php.ini
     
    The PHP Guy, Aug 30, 2009 IP
  3. picos

    picos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #3
    did u test it ??? it is work ? i think, just make 2 connections to 2 databases.
     
    picos, Aug 31, 2009 IP
  4. The PHP Guy

    The PHP Guy Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, I've tested it.

    Another way to do it if your databases aren't too big is:
    1) Open connection to db1
    2) Copy data to a PHP array
    3) Close connection 1
    4) Connect to db2
    5) Insert the array into the db
    6) Close connection 2
     
    The PHP Guy, Sep 2, 2009 IP
  5. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What your friend aren't doing is referencing which connection to use in each query.

    Example: You have $DB1 and $DB2

    
    $query = "SELECT * from sometable";
    $result = mysql_query($query, $DB1);
    while($row = mysql_fetch_object($result)) {
      $query2 = "INSERT INTO anothertable ...";
      $result2 = mysql_query($query2, $DB2);
    } 
    
    PHP:
    See the 2nd parameter for the 2 mysql_query commands - you have to reference which DB connection to run the specific query on, otherwise PHP get's confused and will crash or blow a hissy fit.

    Some web hosts configure PHP to use the most logical db connection (usually the first connection that was created) if you leave out the DB reference, but by default PHP doesn't know what to do if you have multiple db connections open.
     
    Christian Little, Sep 2, 2009 IP
  6. tenev

    tenev Active Member

    Messages:
    322
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #6
    i think mysql_pconnect is better for bigger websites, also and it doesn't close ... btw u can do 2 accounts for the 2 links to $DB1 & $DB2 if you are still unable to use DB1 after connecting DB2 ( i never tested 2 db's.. ... )

    using mysql_pconnect (persistent connection ) will not close the connectiot , and won't create any NEW connection each time you call it with the same parameters, mysql_connect is not for heavy websites...

    http://php.net/function.mysql-pconnect


    this is actually needed only when there are too many users, but it won't be needed if he uses
    mysql_pconnect();
    PHP:
     
    tenev, Sep 2, 2009 IP
  7. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    if you use 2 databases from the same server, then don't change the mysql_connect(), instead, remove the function mysql_select_db(); and instead, query like:
    
    "SELECT * FROM [i]database-name[/i][b].[/b][i]table-name[/i]......................
    
    Code (markup):
     
    yuvrajm, Sep 4, 2009 IP
  8. tenev

    tenev Active Member

    Messages:
    322
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #8
    won't work if the databases are in different SQL accounts...:p
     
    tenev, Sep 15, 2009 IP