SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    connect 2 databases together on one page

    hi all

    i want to use and connect two databases together.

    i m connecting first database file "config.php" through this code

    PHP Code:
    $conn=mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("tally",$conn);
    session_start();
    $unique_id=session_id(); 
    second database file "config2.php" through this code
    PHP Code:
    $conn2=mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("repair_data",$conn2);
    session_start();
    $unique_id=session_id(); 
    in my webpage i m calling them as

    Code:
    require_once("config.php");
    require_once("config2.php");
    i m not able to connect to both dbs together.

    vineet

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Why, what happens? An error?

  3. #3
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not much of a PHP expert but has quite a bit of old programming languages coding experience. So, doesn't including each of them into the same page get the $unique_id only exist once in the session? If so, the $unique_id will be set first to one session_id(), and then to the other. If so, the variable will only contain the session_id() from the last call...?

    If that is the case, just do as yuo did with conn and conn2 - have two different names.

    If not this is the case, what type of errors do you get and at what kind of line in the code?

  4. #4
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would be the case Bagtjesen, as soon as the second connection is set to the variable $unique_id it wipes out the information from the first call. Good catch.

  5. #5
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If both databases are on the same host, you can do selects like this:
    Code:
    SELECT database1.table1.field, database2.table2.field FROM  database1.table1,  database2.table2 WHERE database1.table1.field = database2.table2.field;
    It might not be fast, but it works.


    On the other hand, your PHP is totally wrong.
    #1 you can only start the session once.
    #2 your $unique_id from the first script will be overwritten with the second one.
    #3 your mysql_select_db from the first script will be overwritten by the second one.

  6. #6
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing to think about. If they are related data why aren't they in the same database?


Bookmarks

Posting Permissions

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