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 3 of 3
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    php with 2 databases connection open

    I have a program that will be using 2 different databases I have an included file for each to open the db but how does my mysqlquery know which db to query??

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well, how about:
    Code:
    SELECT * FROM db.table
    [edit:] Thought I should probably contribute a bit more...

    mysql_query() operates on the last initiated link, or the link specified.

    So you could also:
    Code:
    $rc1 = mysql_connect(...);
    $rc2 = mysql_connect(...);
    mysql_select_db("db1",$rc1);
    mysql_select_db("db2",$rc2);
    mysql_query("query",$rc1);
    mysql_query("query",$rc2");
    Depending on your queries...
    Last edited by Mhtml; 02-01-2007 at 01:54 PM.
    Omnis mico antequam dominus Spookster!

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm. the
    $rc2 = mysql_connect(...);
    will not necessarely create a new link (open a connection). You'll need to set the new_link parameter (the fourth parameter in your mysql_connect(...) to True to force a new link to be opened.

    but i don't realy see why you would need two links, unless the db's are on different servers.
    else, you can just swith between db's by putting in a mysql_select_db() each time you need another db to become te active one.
    PHP Code:
    $link mysql_connect(...);
    mysql_select_db("db1"$link);
    $result mysql_query("select blablabla"$link);
    mysql_select_db("db2"$link);
    $result mysql_query("select blablabla"$link); 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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