Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    rwt
    rwt is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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

  • #3
    rwt
    rwt is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, i get it now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •