PHP MYSQL Query Loop Results with FOREACH Possible?

by 7 replies
11
While, DO While, For Loops work very well for iterating results from a MYSQL database call whether you use the older MYSQL or the new MYSQLI method to retrieve the data, doesn't matter.

The question I have, is it possible to get database results using a FOREACH loop?

I have only been able to retrieve ONE record via foreach loop and for some reason it is the second record. I don't know why.

Here is the code I have been playing with:

It gets one field from all records in a table. Doesn't matter really but I just chose one field instead of ALL for testing.

PHP Code:

$qry 
"SELECT member FROM warriors";
         
$rsWar mysql_query($qry$db) or die(mysql_error());
          
//$rslt     = mysql_fetch_assoc($rsWar);
          
$sum   mysql_num_rows($rsWar);
            
            
foreach(
$rsWar as $v){
                echo 
$v;

         }
         
foreach(
$rsWar as $k=>$v){
                echo 
$k." -> ".$v;

         }

foreach(
mysql_fetch_array($rsWar) as $v){
                echo 
$v;

         }
         
foreach(
mysql_fetch_assoc($rsWar) as $v){
                echo 
$v;

         } 
I tried list and each as well but either php exploded or my IDE bugged out on the code above.

FOREACH is designed to iterate through arrays... MYSQL returns results in arrays... Why then can FOREACH NOT be used to parse through mysql results?

Depending how you write the query and send it to MySQL you can get all the results in one go.

While loops, for loops, do while works fantastic but FOREACH sucks for retrieving database data. Why is that?

Anyone able to get FOREACH to work for this?

I could do it by adding a for loop inside the foreach but defeats the purpose when I can just use a for loop.

Any thoughts?
#programming #foreach #loop #mysql #php #query #results
  • Hi Terry,

    I use foreach frequently to display data from the database. But to display all the items, you need to retrieve all the items first by using a while loop (or something like that). Here is a sample:



    For some reason I cannot paste PHP code into my posts.

    I hope that helps.
  • The function mysql_query returns a resource - not the actual data. You must use the 'fetch' functions to retrieve the individual rows.

    The 'foreach' construct is merely a way to iterate through arrays or objects, and is a way of combining 'list' and 'each' for working with arrays.

    I would highly recommend using an addon class like ezSQL ( available at Justin Vincent , or used to be ) to work with databases. It makes programming life so much easier. As a matter of fact, ezSQL is what the WordPress database functions are derived from.
    • Banned
      [DELETED]
      • [1] reply
  • You guys are weird.

    Off the top of my head here, I think it is

    PHP Code:
    WHILE( = mysql_fetch_assoc()) 
    OR is it just

    PHP Code:
    WHILE(mysql_fetch_assoc() 
  • I don't know why most of my post above turned green. I Hope it is still readable if not that's cool, just makes me sound weird if it isn't..

    I edited the post to break up one long run on paragraph which I actually added lines but didn't show up for some reason in the final post. Using the quick edit feature might not be that great an idea for long posts. LOL

Next Topics on Trending Feed