首页 > 解决方案 > php 错误:在布尔值上调用成员函数 fetch_assoc()

问题描述

我编写了一个非常简单的 php 脚本,目标是连接到 mysql 数据库,检索数据并回显数据。脚本如下:

<?php
// Include config file
require_once 'config.php';

// Prepare a select statement
$get_query = "select * from dishes";

$stmt = $mysqli->prepare($get_query);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();

//create array to put the table in
$response = array();
$response['dishes'] = array();

while($dishes = $result->fetch_assoc()){
    //create a temporary array
    $temp = array();

    //inserting the dishes in the temporary array
    $temp['unique_id'] = $dishes['unique_id'];
    $temp['title']=$dishes['title'];

    //push the temporary array inside response
    array_push($response['dishes'],$temp);
}

echo json_encode($response);

?>

在配置文件中是连接到我的数据库所必需的东西。餐桌菜肴非常简单,它有 2 列,一列用于 unique_id,另一列带有标题。该脚本是不言自明的,因为我在其中补充了评论。但是如果我运行它,我会收到以下错误:

PHP 致命错误:未捕获的错误:在布尔值中调用成员函数 fetch_assoc()

点指向此文件的位置。我不明白为什么会这样。谢谢!

标签: phpmysqlmysqli

解决方案


消除

$stmt->store_result();

推荐阅读