Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.