131
131

Reputation: 1385

looping mysql_fetch_array

Okay, so basically, i want to loop mysql_fetch_array inside a foreach loop, like this:

    foreach($groupname as $group)
    {
        $sql2=mysql_query("SELECT * FROM groups WHERE group='$group'");
        $row2=mysql_fetch_array($sql2);
        ?>
        <img src="images/groups/" width="100px" height="100px" /><br />
        <table>
        <tr><td><b>Group: </b></td><td><?php echo $group; ?></td></tr>
        <tr><td><b>Description: </b></td><td><?php echo $row2['description']; ?></td></tr>
        </table><br /><br /><br />
        <?php
    }
    ?>

So when i do that, i get the following mysql error:

  Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...

Any way around this?

Note: Before i do the foreach loop, i do a while loop where i loop through a mysql table, which actually succeed. this is the code snippet for the while loop:

    $groupname=array();
    $sql=mysql_query("SELECT * FROM joined WHERE email='$email'");
    while($row=mysql_fetch_array($sql))
    {
        $groupname[]=$row['group'];
    }

Upvotes: 0

Views: 142

Answers (2)

Sean
Sean

Reputation: 12433

You can always check for errors using mysql_error() ie.

$sql2=mysql_query("SELECT * FROM groups WHERE `group`='$group'") or die(mysql_error());

but it is failing as group is a reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
use backticks around group -

SELECT * FROM groups WHERE `group`='$group'

Upvotes: 1

zerkms
zerkms

Reputation: 254926

group is a mysql keyword so you need to enclose it in backticks:

WHERE `group` = ...

Upvotes: 1

Related Questions