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?
PHP MYSQL Query Loop Results with FOREACH Possible?
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.
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?
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;
}
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?
- Nail Yener
- SteveJohnson
- DEaFeYe Banned
- [1] reply
- Terry Crim
- Terry Crim
Next Topics on Trending Feed
-
11