致命错误:在非对象上调用成员函数fetch_assoc()
我正在尝试执行一些查询以获取有关某些图像的信息页面。我写了一个函数
function get_recent_highs($view_deleted_images=false){
$lower = $this->database->conn->real_escape_string($this->page_size * ($this->page_number - 1));
$query = "SELECT image_id, date_uploaded FROM `images` ORDER BY ((SELECT SUM( image_id=`images`.image_id ) FROM `image_votes` AS score) / (SELECT DATEDIFF( NOW( ) , date_uploaded ) AS diff)) DESC LIMIT " . $this->page_size . " OFFSET $lower"; //move to database class
$result = $this->database->query($query);
$page = array();
while($row = $result->fetch_assoc())
{
try
{
array_push($page, new Image($row['image_id'], $view_deleted_images));
}
catch(ImageNotFoundException $e)
{
throw $e;
}
}
return $page;
}
根据这些图片的受欢迎程度选择页面。我已经编写了一个Database
处理与数据库交互的Image
类,并保存了有关图像的信息。当我尝试运行此程序时,出现错误。
Fatal error: Call to a member function fetch_assoc() on a non-object
$result
是mysqli结果集,所以我对为什么它不起作用感到困惑。
回答:
那是因为您的查询中有错误。
MySQli->query()
错误时将返回false。将其更改为:
$result = $this->database->query($query);if (!$result) {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
如果有错误,那应该抛出异常…
以上是 致命错误:在非对象上调用成员函数fetch_assoc() 的全部内容, 来源链接: utcz.com/qa/422745.html