PHP Recordset和EOF

时间:2013-09-09 20:52:27

标签: php eof recordset

我觉得我知道这一点,但我感到很沮丧,因为我不记得究竟该怎么做。

在PHP中,我需要在无序列表中重复记录集的项目。我可以重复项目,但如果记录集为空,我不希望显示任何内容。现在,如果没有记录,代码仍然显示一个空白列表项,当我只是不想出现任何内容时。

我试过这个:

<?php do { ?>
    <li><a href="#">Content Goes Here</a></li>
<?php } while (!feof($recordsetName) && $row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>

我尝试过这样做,方法是将重复元素放在if / else语句中,如下所示:

<?php if (!feof($recordsetName)) {
    echo ""; }
else do { ?>
    <li><a href="#">Content Goes Here</a></li>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName));
; } ?>

但这也不起作用。任何信息都会有所帮助

1 个答案:

答案 0 :(得分:0)

while($row = mysql_fetch_assoc($recordsetName)) {
    if($row['fieldname'] != '') {
        echo '<li><a href="#">Content Goes Here</a></li>';
    }
}

或者您可以检查查询是否成功...

$result = mysql_query($sql);
if($result) {
    echo '<li><a href="#">Content Goes Here</a></li>';
}

或者如果它返回任何结果......

$results = mysql_fetch_assoc($recordsetName);
if(mysql_num_rows($results) > 0) {
    while($row = mysql_fetch_assoc($results)) {
       echo '<li><a href="#">Content Goes Here</a></li>';
    }
}

此外,您应该使用mysqliPDO,因为mysql已弃用。

编辑:在示例3中添加循环。

相关问题