致命错误:在非对象 in 上调用成员函数 fetch_array()


Fatal error: Call to a member function fetch_array() on a non-object in

无法纠正错误,有人可以帮助我吗?

$sql_project_material = "SELECT * FROM project_material";
if($result_project_material = $connect->query($sql_project_material))
{
    $rows_project_material = $result_project_material->fetch_array();
    $total_project_material = $result_project_material->num_rows;
    $num_project_material = 0;
}
您的

$result_project_material可能为空或查询失败。

尝试一步一步地做,找到错误在哪里,在调用fetch_array()之前检查结果是否为空。

$connect = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connect->connect_errno) {
   printf("Connect failed: %s'n", $connect->connect_error());
   exit();
}
$result_project_material = $connect->query($sql_project_material);
if($total_project_material = $result_project_material->num_rows > 0) {
   while($result_array = $result_project_material->fetch_array()) {
       $rows_project_material[] = result_array;
   }
}else{
   $rows_project_material = array();
   echo "Empty Result";
}