PHP MYSQL Query Loop Results with FOREACH Possible?
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.
$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?
The 2nd Amendment, 1789 - The Original Homeland Security.
Gun control means never having to say, "I missed you."
The 2nd Amendment, 1789 - The Original Homeland Security.
Gun control means never having to say, "I missed you."
:)
:)