php - mysql 中的 store_result() 和 get_result() 返回 false

标签 php mysqli prepared-statement

我几天前编写了一个代码,包括 get_result() 以从我的数据库中接收结果。今天我想添加到它并修复一些错误。所以我尝试使用 num_rows 来查看是否返回了任何内容。但是为此我不得不使用 store_result()。当我这样做时,get_result() 只返回一个 bool 值 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;
}

最佳答案

使用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;
}

关于php - mysql 中的 store_result() 和 get_result() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253407/

相关文章:

javascript - 不允许使用 Codeigniter/PHP(405 错误)在 jQuery Blueimp FileUpload 上删除方法?

php - PEAR - 仍然相关吗?

php - mysqli::get_result 有什么问题?

php - 存在撇号时使用 json_encode 破坏 JSON 对象

sql - JavaDB 匿名列名

php - Wordpress:在表中搜索元素

php - 在 PHP 中确保传统转义函数 SQL 注入(inject)安全?

php - 为什么 MySQL 语句被忽略?

PHP PDO 准备语句语法问题

php - CodeIgniter 查询构建