【问题标题】:store_result() and get_result() in mysql returns falsemysql 中的 store_result() 和 get_result() 返回 false
【发布时间】:2015-10-06 03:24:25
【问题描述】:

几天前我编写了一个代码,包括get_result(),以从我的数据库中接收结果。今天我想添加它并修复一些错误。所以我尝试使用num_rows 来查看是否有任何返回。但为此我不得不使用store_result()。当我这样做时,get_result() 只会返回一个布尔值 false。当我注释掉store_result() 时,一切正常。 我知道>= 会搞砸的。但我把= 放在那里进行调试(注释掉store_result() 看看发生了什么)。所以这不是问题

$sql = $this->connect();
$a = $sql->prepare("SELECT `name`, `title`, `comment`, `date`  FROM `comment` WHERE `post`=?");
$a->bind_param("s", $id);
$a->execute();
$a->store_result();
if ($a->num_rows >= 0) {
    $res = $a->get_result();
    var_dump($res);

    while ($row = $res->fetch_assoc()) {
        $results[] = $row;
    }
    return $results;
} else {
    return false;
}

【问题讨论】:

    标签: php mysqli prepared-statement


    【解决方案1】:

    使用get_result() 代替 store_result(),然后使用结果对象的num_rows

    $a->execute();
    $res = $a->get_result();
    if ($res->num_rows > 0) {
        while ($row = $res->fetch_assoc()) {
            $results[] = $row;
        }
        return $results;
    } else {
        return false;
    }
    

    【讨论】:

    • 一小时前做了那件事。那时没用.. 但现在:D 谢谢
    • 现在我记得我为什么不使用那个方法了。当你在 get_result() 上使用 num_rows 时,无论检索到多少,你总是得到 0
    • 也许这个文档是相关的:对于无缓冲的结果集,mysqli_num_rows() 在检索到结果中的所有行之前不会返回正确的行数。。我不知道如何切换到缓冲结果集。
    • 找到了我遇到的问题。我试图获取错误变量的 num 行。非常坦克你:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多