加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在使用$stmt之前检查结果 – > fetch()

发布时间:2020-12-13 21:46:25 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Why does mysqli num_rows always return 0?????????????????????????????????????2个 可能是一个愚蠢的问题.我有以下代码: while ($stmt - fetch()) { echo "pstrong" .$name. "/strongbr /" .$comment. "/p";} 如果$stmt – 我想执行while.
参见英文答案 > Why does mysqli num_rows always return 0?????????????????????????????????????2个
可能是一个愚蠢的问题.我有以下代码:

while ($stmt -> fetch()) {
    echo "<p><strong>" .$name. "</strong><br />" .$comment. "</p>";
}

如果$stmt – >我想执行while. fetch()至少返回一行(这是一个SELECT查询)..但是如果我这样做的话

if ($stmt -> fetch())

它已经执行了一次,我想避免这种情况.任何线索?

提前致谢.

解决方法

http://php.net/manual/en/mysqli-stmt.num-rows.php

尝试使用$stmt-> num_rows> 0

$stmt->store_result();
if( $stmt->num_rows > 0 )
{
    while( $stmt->fetch() ) {
        echo "<p><strong>" .$name. "</strong><br />" .$comment. "</p>";
    }
}

为什么mysqli_result有一个fetchAll函数和mysqli_stmt不….我不知道.

更长,但可能会奏效

$result = $stmt->fetch();
if( !empty( $result )
{
    do
    {
        echo "<p><strong>" .$name. "</strong><br />" .$comment. "</p>";
    } while( $result = $stmt->fetch() ); 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读