Jessica Posted January 8, 2007 Share Posted January 8, 2007 I have two databases. One has a table items, and another has a table news. (example.)I want to connect to both in my script (in my config file, so at the beginning of every page.)When I connect to the first, and query the items table, it works fine. I can print out the results, so I know it worked.I then connect to the second, query the news table, and that works.If I try to connect to both at the beginning, the first connection gets replaced by the second. I used to be able to do this with different users, but after I switched servers I now have to use the same user and password to access both.Is that why this is not working? Any ideas how I can do this so I don't have to combine the databases? I cannot use a second user (I have already pleaded with (mt) and got a no.)(In reality, there are 4 databases and they are all very big, so I don't want to combine them if at all possible.)Sample Code:[code]<?$db_username = 'username';$db_password = 'password';$db_database = 'database_1';$db_server = 'myhost';$dbOrig = mysql_connect($db_server, $db_username, $db_password);if(!$dbOrig){ die('Could not connect: ' . mysql_error());}mysql_select_db($db_database, $dbOrig);$db_database = 'database_2';$dbMain = mysql_connect($db_server, $db_username, $db_password);if(!$dbMain){ die('Could not connect: ' . mysql_error());}mysql_select_db($db_database, $dbMain);$sql = "SELECT * FROM items";$rs = mysql_query($sql, $dbOrig);if($rs){ while ($row = mysql_fetch_array($rs)) { print_r($row); }}else{ die('Error: ' . mysql_error());}$sql = "SELECT * FROM news";$rs = mysql_query($sql, $dbMain);if($rs){ while ($row = mysql_fetch_array($rs)) { print_r($row); }}else{ die('Error: ' . mysql_error());}?>[/code]Output: Error: Table 'database_2.items' doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/33376-solved-cannot-connect-to-two-databases-at-same-time/ Share on other sites More sharing options...
kenrbnsn Posted January 8, 2007 Share Posted January 8, 2007 Instead of having two databases, maybe you want one database with two tables.Ken Quote Link to comment https://forums.phpfreaks.com/topic/33376-solved-cannot-connect-to-two-databases-at-same-time/#findComment-155962 Share on other sites More sharing options...
Jessica Posted January 8, 2007 Author Share Posted January 8, 2007 [quote author=jesirose link=topic=121547.msg499917#msg499917 date=1168282262](In reality, there are 4 databases and they are all very big, so I don't want to combine them if at all possible.)[/quote]No, I don't want one database. It would have a hell of a lot of tables. Not just two. Quote Link to comment https://forums.phpfreaks.com/topic/33376-solved-cannot-connect-to-two-databases-at-same-time/#findComment-155964 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 If both databases are on the same server you don't need a new connection, just use mysql_select_db to select the database you want to use before you use it. Quote Link to comment https://forums.phpfreaks.com/topic/33376-solved-cannot-connect-to-two-databases-at-same-time/#findComment-156030 Share on other sites More sharing options...
Jessica Posted January 8, 2007 Author Share Posted January 8, 2007 *headdesk*I can't believe that didn't occur to me thorpe. I just have to add a bit to my db wrapper and that will work.You rock :) Quote Link to comment https://forums.phpfreaks.com/topic/33376-solved-cannot-connect-to-two-databases-at-same-time/#findComment-156033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.