Results 1 to 6 of 6

Thread: connecting to multiple databases on one page

  1. #1

    connecting to multiple databases on one page

    Ok i have several includes that are connecting to my forum database to pull recent posts and news. Ive got another include that is connecting to another database that has a table for tutorial and article pages that I want to display. The problem im getting is that, without using mysql_close, the tutorial script is looking in the forum database, so im guessing that the connection is staying open. When i put mysql_close at the end of each script it only runs the first include and the next one says database not found. Can someone help plz

  2. #2
    mysql_select_db("db1");
    mysql_select_db("db2");

    $res_db1 = mysql_query("select * from db1.foobar");
    $res_db2 = mysql_query("select * from db2.foobar");

  3. #3
    i already have 2 different connect files, one for the first database and one for the second.

    could you explain a little bit, im not an expert at php.

  4. #4
    sure, sorry for being so straight
    it doesn't matter how many connections you have, and even if they are on same file or separate files. you set up your connections with
    $connection1 = mysql_connect ($host, $user, $pass);
    $connection2 = mysql_connect ($host2, $user, $pass);

    Now you select the 2 databases, each one with its own connection
    mysql_select_db($database1, $connection1);
    mysql_select_db($database2, $connection2);

    If both databases are on the same server, you can use the same connection or even don't tell which connection to use, because if you only set up one, php will use always the same.

    when you execute a query, you must write the database name in front of the tables that you want to use.
    examples:
    if you want to query a table news located at database1 you create a query like
    $rsNews = mysql_query("SELECT * FROM database1.news");
    next you want to query a table named tutorials located at database 2:
    $rsTutorials = mysql_query("SELECT * FROM database2.tutorials");

    now you have 2 recordsets $rsNews and $rsTutorials ready to use

    hth

  5. #5
    $rsNews = mysql_query("SELECT * FROM database1.news");
    next you want to query a table named tutorials located at database 2:
    $rsTutorials = mysql_query("SELECT * FROM database2.tutorials");
    ahaaaaaa, i wasn't putting the database name infrom of the table after the FROM. Thanks a lot man, that really helped

  6. #6
    glad to hear that! this makes me happy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012