brooksh Posted January 21, 2008 Share Posted January 21, 2008 I am trying to combine two different queries but I can only seem to get the first query to display. It only comes up with the first state. Here is my code. $sql = "SELECT DISTINCT state from cities where country = 'US' ORDER BY state"; $result = mysql_query ($sql); while($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $state = $row[state]; $arr = array($state); reset($arr); while (list(, $value) = each($arr)) { } } foreach ($arr as $key => $value) { $sql = "SELECT cities from cities where country='US' AND state='$value'"; $result = mysql_query ($sql); while($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $cities = $row[cities]; } } Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/ Share on other sites More sharing options...
revraz Posted January 21, 2008 Share Posted January 21, 2008 One thing you should do is use mysql_error() after your queries so you can see if there is a error in the query. Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/#findComment-445377 Share on other sites More sharing options...
rhodesa Posted January 21, 2008 Share Posted January 21, 2008 I think what you are looking for is something like this: $sql = "SELECT DISTINCT state from cities where country = 'US' ORDER BY state"; $result = mysql_query($sql); $states = array(); while($row = mysql_fetch_array ($result, MYSQL_ASSOC)){ $states[] = $row[state]; } $cities = array(); foreach ($states as $value) { $sql = "SELECT cities from cities where country='US' AND state='$value'"; $result = mysql_query ($sql); while($row = mysql_fetch_array ($result, MYSQL_ASSOC)){ $cities[] = $row[cities]; } } It would help if you specified exactly what you are trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/#findComment-445381 Share on other sites More sharing options...
brooksh Posted January 21, 2008 Author Share Posted January 21, 2008 I would like to display each city in the database from every state in the US. table 1 "state - CA, cities - Los Angeles, San Jose, San Francisco" table 2 "state - WA, cities - Olympia, Seattle, Tacoma" Displayed as: Los Angeles Olympia San Jose San Francisco Seattle Tacoma Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/#findComment-445414 Share on other sites More sharing options...
rhodesa Posted January 21, 2008 Share Posted January 21, 2008 It seems like a nested loop would be better: $s_sql = "SELECT `state` FROM `cities` WHERE `country` = 'US' ORDER BY `state`"; $s_result = mysql_query($s_sql); while($state = mysql_fetch_assoc($result)){ echo "Current state: {$state['state']}\n"; $c_sql = "SELECT cities from cities where country='US' AND state='$value'"; $c_result = mysql_query ($c_sql); while($city = mysql_fetch_assoc($c_result)){ echo " -{$city['cities']\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/#findComment-445426 Share on other sites More sharing options...
brooksh Posted January 21, 2008 Author Share Posted January 21, 2008 Doesn't seem to work for me. I'm running PHP 4.3 I got these two errors Warning: Unexpected character in input: '\' (ASCII=92) state=1 Parse error: syntax error, unexpected T_STRING, expecting '}' Quote Link to comment https://forums.phpfreaks.com/topic/87082-merging-two-different-queries-together/#findComment-445499 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.