Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    London, Uk
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Foreach for outputting a column of data

    Is there anywhere to achieve this with a foreach loop?

    Code:
     
    $query = "SELECT * FROM foods WHERE food_id=1 ORDER BY position ASC";
    $result_set = mysql_query($query);
    	
    while ($fruits = mysql_fetch_array($result_set)){
    				echo $fruits["name"] . "<br />"
    }
    This works and outputs:
    Code:
    Apple
    Banana
    Orange
    but for learning purposes I would love to know how to do the same with a foreach loop.
    I have tried: foreach($fruits["name"] as $singlefruit){
    echo $singlefruit . "<br />"; }

    but get "Invalid argument supplied for foreach()"

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, first, this is a PHP issue, not a MySQL one.

    Next, you mean you replaced the while loop with the foreach you showed? That couldn't work, as you haven't defined $first["name"] anywhere. The standard mysql_fetch_array output doesn't work in a foreach, as it doesn't return the full set of values nor is it designed to work in a foreach. You could do a while loop to store all the data into $fruits then run the foreach, but why? You'd just be looping twice for no apparent reason.

    And why do you want to run a foreach instead of a while? Doesn't seem to be any purpose to it?

  • #3
    New Coder
    Join Date
    Feb 2012
    Location
    London, Uk
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, I think you answered my question..I'll just use while loops but wanted a better understanding of foreach loops.

    Well, first, this is a PHP issue, not a MySQL one.
    The subheading of this forum section says:
    PHP questions as they relate to mySQL should also be posted here.
    so since this has to do with processing mysql tables and columns using php it's perfect for this section.

    Question:
    The standard mysql_fetch_array output doesn't return the full set of values
    I thought it turned ALL the rows from the mysq_query into a usable array. Which data does it miss out then..or does it just return a single row?

    The foreach example I provided had:
    $fruits = mysql_fetch_array($result_set);
    above it..does that make a difference?
    I know that the foreach loop has 2 different syntax's so maybe it's the other type, the foreach($array as $key => $value) that would be useful here...
    Last edited by Link187; 03-13-2012 at 07:33 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Its not bad you posted here, that's not what I meant. I was just clarifying that its not a MySQL issue.

    And no, mysql_fetch_array simply pulls row currently being pointed at in the result set, and then advances the pointer to the next row. In your example, $result_set contains all the returned rows, but mysql_fetch_array only returns one row. It won't work in foreach, regardless of the syntax you use. foreach requires an array (or in your case, a multidimentional array).

  • Users who have thanked Keleth for this post:

    Link187 (03-13-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •