Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2007, 07:51 PM   PM User | #1
rwt
New Coder

 
Join Date: Jul 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
rwt is an unknown quantity at this point
foreach loop on mySQL

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
rwt is offline   Reply With Quote
Old 08-11-2007, 08:13 PM   PM User | #2
mcjwb
Regular Coder

 
Join Date: Jul 2007
Location: UK
Posts: 223
Thanks: 0
Thanked 14 Times in 14 Posts
mcjwb is an unknown quantity at this point
Your code should be:
PHP Code:
$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!

Last edited by mcjwb; 08-11-2007 at 08:14 PM.. Reason: fixed code
mcjwb is offline   Reply With Quote
Old 08-11-2007, 08:57 PM   PM User | #3
rwt
New Coder

 
Join Date: Jul 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
rwt is an unknown quantity at this point
thanks, i get it now.
rwt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 PM.


Advertisement
Log in to turn off these ads.