循环mysql_fetch_array

时间:2013-07-10 01:15:16

标签: php mysql foreach

好的,基本上,我想在foreach循环中循环mysql_fetch_array,如下所示:

    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
    }
    ?>

所以当我这样做时,我得到以下mysql错误:

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

有什么方法吗?

注意:在我做foreach循环之前,我做一个while循环,我循环通过一个mysql表,实际上成功了。这是while循环的代码片段:

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

2 个答案:

答案 0 :(得分:1)

groupmysql keyword,因此您需要将其括在反引号中:

WHERE `group` = ...

答案 1 :(得分:1)

您始终可以使用mysql_error()

来检查错误
$sql2=mysql_query("SELECT * FROM groups WHERE `group`='$group'") or die(mysql_error());

但失败了,因为group是保留字http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
group -

周围使用反引号
SELECT * FROM groups WHERE `group`='$group'