JSHINER Posted April 27, 2007 Share Posted April 27, 2007 I have the following: function List($db, $zip = false) { $db->selectDb('database2'); $town = $db->getRow("SELECT * FROM towns_zipcodes, towns WHERE town_zipcodes.zip = '$zip' AND towns_zipcodes.town_id = towns_zipcodes.zip AND towns.id = towns_zipcodes.towns_id AND towns.id = towns.name LIMIT 1"); return $town; } The page this is for is connected to database1, but I need to query databse2 to get these results. What is the correct way to query another database while connected to database1? Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/ Share on other sites More sharing options...
JSHINER Posted April 27, 2007 Author Share Posted April 27, 2007 And ideas? Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/#findComment-239984 Share on other sites More sharing options...
tauchai83 Posted April 28, 2007 Share Posted April 28, 2007 i never try that before. But why you want to connect to 2 DB at once? 1 DB stores many tables which is good enough. to connect DB, we need to establish connection. So if you want 2 DB, you are likely to need to make connection to DB twice. but this is not encouragable. Using one DB and have many tables inside is good enough. tell me the reason you want 2 DB. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/#findComment-240150 Share on other sites More sharing options...
fert Posted April 28, 2007 Share Posted April 28, 2007 what are you using to connect to the databases? Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/#findComment-240151 Share on other sites More sharing options...
JSHINER Posted April 28, 2007 Author Share Posted April 28, 2007 I need to connect to two because one has a lot of tables, and so does the second. And they are used for different things. To connect to the first I use the standard mysql_connect. However, while under that connection, I need to open a new connection to retrieve data to display on the same page. I tried using mysql_select_db but it did not work. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/#findComment-240268 Share on other sites More sharing options...
fert Posted April 28, 2007 Share Posted April 28, 2007 $cn1=mysql_connect("host","user","password"); $cn2=mysql_connect("Host","user","password"); mysql_select_db("db1",$cn1); mysql_select_db("db2",$cn2); Quote Link to comment https://forums.phpfreaks.com/topic/48960-solved-connecting-to-two-databases-at-the-same-time/#findComment-240330 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.