SELECT上准备语句时的PHP错误(fetch_assoc()函数)

时间:2017-06-07 07:30:10

标签: php mysqli

这是一个常见的问题,但我已经完成了我的研究,但我无法找到解决方案。所以我在这里。

我得到的错误是:致命错误:在非对象上调用成员函数fetch_assoc()

首先我启用了mysqlnd。其次我的查询没有错误。我能够在phpmyadmin中运行它没有任何问题。

以下是正在运行的查询:

$stmt = $this->conn->prepare($query);
if($stmt){
    $stmt->execute();
    while($row = $stmt->get_result()->fetch_assoc()){
        $photo = new Photo(null, null, null, null);
        $photo->id=$row["id"];
        $photo->userId=$row["user_id"];
        $photo->name = $row["name"];
        $photo->photoPath=$row["photo_path"];
        $result[] = $photo;
    }
    return $result;
}else{
    var_dump($this->db->error);
}

以下是发生错误的代码(更具体地说,在fetch_assoc()函数中):

{{1}}

如果你们能帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:0)

你应该只调用get_result()一次,而不是每次都通过循环。

$stmt->execute();
$stmt_result = $stmt->get_result();
while($row = $stmt_result->fetch_assoc()){
    $photo = new Photo(null, null, null, null);
    $photo->id=$row["id"];
    $photo->userId=$row["user_id"];
    $photo->name = $row["name"];
    $photo->photoPath=$row["photo_path"];
    $result[] = $photo;
}
相关问题