RON_ron Posted September 6, 2012 Share Posted September 6, 2012 I've 2 databases with a lot of records. I need a way to pull all the records of my_db2 and at the same time to pull the relevant record from my_db2 as a string.(e.g. NAME and CITY and ELIGIBILITY.). This is my code. But not outputting the way I want. (outputs only the first record). $query1 = "SELECT Name, City FROM my_db1 ORDER BY City DESC"; $results1 = mysql_query($query1); while($line1 = mysql_fetch_array($results1)) { $person = $line1["Name"]; echo $line1["Name"]." and "; echo $line1["City"]." and "; } $query2 = "SELECT eligibility FROM my_db2 WHERE name=$person"; $results2 = mysql_query($query2); while($line2 = mysql_fetch_array($results2)) { echo $line2["eligibility"]."."; } Quote Link to comment https://forums.phpfreaks.com/topic/268056-matching-data_from_two/ Share on other sites More sharing options...
spiderwell Posted September 6, 2012 Share Posted September 6, 2012 your loops should be nested, not one after another, so you are currently looping through ALL the db1 records, leaving only the last record to populate $person, so the second loop will only output 1 record Quote Link to comment https://forums.phpfreaks.com/topic/268056-matching-data_from_two/#findComment-1375704 Share on other sites More sharing options...
RON_ron Posted September 6, 2012 Author Share Posted September 6, 2012 I tried this but I'm not quite sure what's wrong? Could someone help? $query = "SELECT * FROM my_db1 LEFT OUTER JOIN my_db2 ON my_db1.City = my_db2.name ORDER BY my_db1.City DESC"; $results= mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/268056-matching-data_from_two/#findComment-1375707 Share on other sites More sharing options...
Barand Posted September 6, 2012 Share Posted September 6, 2012 In your first post you are matching name with name, as expected $query = "SELECT * FROM my_db1 LEFT OUTER JOIN my_db2 ON my_db1.name = my_db2.name ORDER BY my_db1.City DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/268056-matching-data_from_two/#findComment-1375713 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.