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

时间:2015-01-05 16:14:50

标签: php sql arrays fatal-error

找不到错误,有人可以帮帮我吗?

$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;
}

1 个答案:

答案 0 :(得分: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";
}