...

View Full Version : foreach loop on mySQL



rwt
08-11-2007, 08:51 PM
hello all.
I am trying to echo a table with a data for each entry in the mySQL.
I don't understand why I keep getting Invalid argument supplied for foreach() !

first I get the mySQL table:
$fetchinfo = mysql_query("SELECT * FROM users WHERE isAdmin=true ;");
$fetcharray = mysql_fetch_array($fetchinfo);
I only want to select those where isAdmin is true.

Then I create my simple table:
echo '<table border="0" cellspacing="0" cellpadding="0">';
foreach($fetcharray){
echo'<tr>';
foreach($fetcharray['Name'] as $Name){
echo'<td>$Name</td>';
}
foreach($fetcharray['LastName'] as $LastName){
echo'<td>$LastName</td>';
}
echo'</tr>';
}
echo'</table>';

What are the errors?

thanks

mcjwb
08-11-2007, 09:13 PM
Your code should be:

$fetchinfo = mysql_query("SELECT * FROM users WHERE isAdmin=true ;");

echo '<table border="0" cellspacing="0" cellpadding="0">';
while($fetcharray = mysql_fetch_array($fetchinfo)){
echo'<tr>';
echo'<td>'.$fetcharray['Name'].'</td>';
echo'<td>'.$fetcharray['LastName'].'</td>';
echo'</tr>';
}
echo'</table>';


You don't need or want to use foreach anywhere here!

rwt
08-11-2007, 09:57 PM
thanks, i get it now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum