問題描述
我正在嘗試執行一些查詢以獲取有關某些圖像的信息頁面.我寫了一個函數
I'm trying to execute a few queries to get a page of information about some images. I've written a function
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
類.當我嘗試運行此程序時出現錯誤.
that selects a page of these images based on their popularity. I've written a Database
class that handles interactions with the database and an Image
class that holds information about an image. When I attempt to run this I get an error.
Fatal error: Call to a member function fetch_assoc() on a non-object
$result
是一個 mysqli 結果集,所以我很困惑為什么這不起作用.
$result
is a mysqli resultset, so I'm baffled as to why this isn't working.
推薦答案
那是因為您的查詢中有錯誤.MySQli->query()
出錯時返回false.將其更改為類似::
That's because there was an error in your query. MySQli->query()
will return false on error. Change it to something like::
$result = $this->database->query($query);
if (!$result) {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
如果有錯誤應該拋出異常...
That should throw an exception if there's an error...
這篇關于致命錯誤:在非對象上調用成員函數 fetch_assoc()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!