brooksh Posted January 21, 2008 Share Posted January 21, 2008 I know this should be something simple, but I can't figure it out. Can someone please help me? I have a table name cities with a field named cities that has "Austin,Los Angeles,New York,San Francisco,Seattle" I want to grab each city from the field and display it. Here is what I have, but it is obviously wrong. $query = "SELECT * FROM cities WHERE country='US'"; $result = mysql_query($query); $rows = mysql_num_rows($result); $i = 0; while ($i < $rows){ $cities = $rows[cities]; $cities = explode(",", $cities); $name = mysql_result($result, $i, "city"); echo "$name<br>"; $i++; } Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/ Share on other sites More sharing options...
mmarif4u Posted January 21, 2008 Share Posted January 21, 2008 If u want to simple show the cities,then use his simple code: $query = "SELECT * FROM cities WHERE country='US'"; $result = mysql_query($query); $num = mysql_num_rows($result); while ($rows=mysql_fetch_object($result)){ $cities = $rows->cities; $cities = explode(",", $cities); echo $cities."<br>"; } Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444893 Share on other sites More sharing options...
brooksh Posted January 21, 2008 Author Share Posted January 21, 2008 It still comes up as an array. Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444903 Share on other sites More sharing options...
mmarif4u Posted January 21, 2008 Share Posted January 21, 2008 So how u want to show it,please explain it a bit. Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444907 Share on other sites More sharing options...
Ken2k7 Posted January 21, 2008 Share Posted January 21, 2008 Is the column name cities? If so, try: $query = "SELECT cities FROM cities WHERE country='US'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)){ $cities = $row['cities']; $cities = explode(",", $cities); echo "$name <br>"; } Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444916 Share on other sites More sharing options...
Barand Posted January 21, 2008 Share Posted January 21, 2008 If there is only a single record with country='US' <?php $query = "SELECT cities FROM cities WHERE country='US'"; $result = mysql_query($query); $cities = explode (',' , $mysql_result ($result, 0)); foreach ($cities as $city) { echo "$city<br/>"; } Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444917 Share on other sites More sharing options...
brooksh Posted January 21, 2008 Author Share Posted January 21, 2008 Thank you it works. $result = mysql_query($query); $cities = explode (',' , mysql_result($result, 0)); foreach ($cities as $city) { echo "$city<br/>"; } Link to comment https://forums.phpfreaks.com/topic/86999-solved-need-help-with-array-and-foreach/#findComment-444923 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.