php+mysqli while(false !== ($data = $mysqli_res->fetch_assoc)) 创建无限循环
我在管理面板中写了新评论的概述。 我的代码
if($res = $db->query("select * from comments where unlocked=0"));
{
$o = 0; //abortcondition temp
echo '<div class="newcomments"><a>neuste kommentare: </a><br />';
//here is the infinite loop
while(false !== ($data = $res->fetch_assoc()) && ++$o < 10)
{
if($articles = $db->query("select title from coreless_articles where ID=".$data['article']))
{
if(false !== ($article = $articles->fetch_assoc()))
{
echo
'<div id="comment'.$data['ID'].'">'.$data['from'].' in'.
' <a href="article/'.($article['uri']).'#comments">'.$article['title'].'</a>'.
' <a href="javascript:;">anzeigen</a>'.
' <a href="javascript:;">freischalten</a>'.
' <a href="javascript:;">löschen</a>'.
' <div class="preview" >'.$data['text'].'</div>'.
'</div>';
}
}
}
}
如果我没有 $o 中止条件, 会创建无限循环。但我想显示所有新评论,而不仅仅是 ++$o <名词我使用 mysqli 访问数据库。
有什么想法吗?
I wrote an overview for new comments in an admin panel. My code
if($res = $db->query("select * from comments where unlocked=0"));
{
$o = 0; //abortcondition temp
echo '<div class="newcomments"><a>neuste kommentare: </a><br />';
//here is the infinite loop
while(false !== ($data = $res->fetch_assoc()) && ++$o < 10)
{
if($articles = $db->query("select title from coreless_articles where ID=".$data['article']))
{
if(false !== ($article = $articles->fetch_assoc()))
{
echo
'<div id="comment'.$data['ID'].'">'.$data['from'].' in'.
' <a href="article/'.($article['uri']).'#comments">'.$article['title'].'</a>'.
' <a href="javascript:;">anzeigen</a>'.
' <a href="javascript:;">freischalten</a>'.
' <a href="javascript:;">löschen</a>'.
' <div class="preview" >'.$data['text'].'</div>'.
'</div>';
}
}
}
}
creates an infinite loop, if I don't have the $o abort condition. But I want to show all new comments, not just ++$o < n. I use mysqli to access the database.
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mysqli_result::fetch_assoc 当为空时返回 NULL,而不是 FALSE。 http://www.php.net/manual/en/mysqli -result.fetch-assoc.php
只需将您的测试更改为 NULL !== (etc) 即可。
(这非常令人困惑,因为等效函数 mysql_fetch_assoc() 确实返回 FALSE。)
mysqli_result::fetch_assoc returns NULL when it's empty, not FALSE. http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
Just change your test to
NULL !== (etc)
.(This is pretty confusing because the equivalent function mysql_fetch_assoc() does return FALSE.)
为什么要进行阴性检测?为什么不直接检测一下是否呈阳性呢?
无论如何,@evan 对于代码创建无限循环的解释是绝对正确的。
Why are you testing for the negative condition? Why don't you simply test for the positive one?
Anyway,
@evan
's explatation why the code creates an infinite loop holds absolutely true.