Drewser33 Posted July 7, 2011 Share Posted July 7, 2011 This should be simple, but I can't seem to get it right. I am using a query to select values, it returns a list of 7 results. I would like to use a foreach for those results. Below is how I understand it (at this point just trying to view the results) but I am getting nothing. I am new to arrays and the foreach. $query = "SELECT a FROM table WHERE b IS NULL"; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_array($result); foreach($row['a'] as $value) { echo $value.'<br>'; } Thanks Link to comment https://forums.phpfreaks.com/topic/241342-query-array/ Share on other sites More sharing options...
freelance84 Posted July 7, 2011 Share Posted July 7, 2011 http://php.net/manual/en/function.mysql-fetch-array.php When i was learning i found it easy to imagine this: A query will return a stack of rows from the table in a special array, like a cd rack holding cd's (where the cd's are the results and the rack it self being the array container) To retrieve these rows, you must get one row at a time, and once you have retrieved them they will no longer be in that special array (similar to the cd analogy, if you take a cd from a rack it is no longer there). So what you are wanting to do is loop through the entire returned result extracting and processing one row at a time... thus what you have done in your script is return one row into a variable named $row. Check out the examples on the php manual. Hope this helps Link to comment https://forums.phpfreaks.com/topic/241342-query-array/#findComment-1239685 Share on other sites More sharing options...
AbraCadaver Posted July 7, 2011 Share Posted July 7, 2011 http://php.net/manual/en/function.mysql-fetch-array.php When i was learning i found it easy to imagine this: A query will return a stack of rows from the table in a special array, like a cd rack holding cd's (where the cd's are the results and the rack it self being the array container) To retrieve these rows, you must get one row at a time, and once you have retrieved them they will no longer be in that special array (similar to the cd analogy, if you take a cd from a rack it is no longer there). So what you are wanting to do is loop through the entire returned result extracting and processing one row at a time... thus what you have done in your script is return one row into a variable named $row. Check out the examples on the php manual. Hope this helps The rows remain in the result. You must start back at the first row to retrieve them again: http://us2.php.net/manual/en/function.mysql-data-seek.php Link to comment https://forums.phpfreaks.com/topic/241342-query-array/#findComment-1239687 Share on other sites More sharing options...
freelance84 Posted July 7, 2011 Share Posted July 7, 2011 ha. Didn't know that! Cheers Link to comment https://forums.phpfreaks.com/topic/241342-query-array/#findComment-1239688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.