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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using 2 Seperate Database Connections

    I am using a site that connects to a remote database to get all the info from. I am trying to also connect to my own database so I can combine information. Is that possible?

    Here is my remote connection:

    PHP Code:
    $dbhost "remotesite.com";
    $dbuser "db_user";
    $dbname "db_name";
    $dbpass "password";
    $db mysql_connect("$dbhost","$dbuser","$dbpass");
    mysql_select_db("$dbname",$db); 
    When I try this:

    PHP Code:
    $dbhost2 "localhost";
    $dbuser2 "db_user";
    $dbname2 "db_name";
    $dbpass2 "password";
    $db2 mysql_connect("$dbhost2","$dbuser2","$dbpass2");
    mysql_select_db("$dbname2",$db2); 
    Only one database is connected but not the other.
    Last edited by Pee-H-Pee; 03-05-2008 at 01:44 AM. Reason: Fix Typo in Code

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Wouldn't it be easier to export the data from one host/database and import it into the other?

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, I can't do that...
    So does that mean it can't be done?

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    In the 2nd code snippet, I believe:
    PHP Code:
    $dbhost "localhost"
    Should be:
    PHP Code:
    $dbhost2 "localhost"

  • #5
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh wow, I didn't notice the typo...
    I'll try it that way and see...

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    hes correct i spotted that off the mark, this kind of check should be done before posting, hope that it works for you though

  • #7
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It still didn't work... only one database works but not the other...

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Pee-H-Pee View Post
    It still didn't work... only one database works but not the other...
    1. Which one isn't working?
    2. How do you know it isn't working? Errors??
    3. Add error checking. (See the manual)
    4. Does the one that isn't working allow remote connections? I believe it's disabled by default for security reasons.

  • #9
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It seems whichever one I have on the bottom is the one that works... If I switch them it's still like that. So they are conflicting for some reason.

    The one that doesn't work is checking for tables on the one that is working, which is a seperate db connection, and the error says the talble does not exist.

  • #10
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Can you give us a bit more code, like queries? How are you checking which one is working etc?

  • #11
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by Pee-H-Pee View Post
    , and the error says the talble does not exist.
    Doesn't this say it all?
    check table name, check table does exist and let us know.

  • #13
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Pee-H-Pee View Post
    It seems whichever one I have on the bottom is the one that works... If I switch them it's still like that. So they are conflicting for some reason.

    The one that doesn't work is checking for tables on the one that is working, which is a seperate db connection, and the error says the talble does not exist.
    Wait, are you using the same username/password to access both databases? If so you need to add a boolean true to your connect call, after the password:
    PHP Code:
    $db mysql_connect($dbhost$dbuser$dbpasstrue);
    $db2 mysql_connect($dbhost2$dbuser2$dbpass2true); 
    By default PHP will reuse the first connection if you use the same login information.

  • #14
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, this is the db connection my site mainly runs off of.
    The majority of the content is from a remote db and everything works perfectly fine the way it should be...

    PHP Code:
    $dbhost "remotesite.com";
    $dbuser "db_user";
    $dbname "db_name";
    $dbpass "password";
    $db mysql_connect("$dbhost","$dbuser","$dbpass");
    mysql_select_db("$dbname",$db); 
    Now I want to add content from my own database so I am using this:

    PHP Code:
    $dbhost2 "localhost";
    $dbuser2 "db_user";
    $dbname2 "db_name";
    $dbpass2 "password";
    $db2 mysql_connect("$dbhost2","$dbuser2","$dbpass2");
    mysql_select_db("$dbname2",$db2); 
    This is what my config file looks like:

    PHP Code:
    // Remote DB connection
    $dbhost "remotesite.com";
    $dbuser "db_user";
    $dbname "db_name";
    $dbpass "password";
    $db mysql_connect("$dbhost","$dbuser","$dbpass");
    mysql_select_db("$dbname",$db);

    // My DB connection
    $dbhost2 "localhost";
    $dbuser2 "db_user";
    $dbname2 "db_name";
    $dbpass2 "password";
    $db2 mysql_connect("$dbhost2","$dbuser2","$dbpass2");
    mysql_select_db("$dbname2",$db2); 
    Once I add the bottome part in the config file to add the connection to my own database it brings up the data but then the remote connection breaks...

    Instead of bringing up the data for the remote connection it is checking for the table on my own database.

    Table 'my_connection.remote_table' doesn't exist

    Each DB connection has it's own different username and password.
    Last edited by Pee-H-Pee; 03-05-2008 at 03:12 AM.

  • #15
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You sure its not remote_table rather than romote_table?
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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