gpt4 book ai didi

php - fatal error : Call to a member function fetch_assoc() on a non-object

转载 作者:行者123 更新时间:2023-11-29 21:15:41 24 4
gpt4 key购买 nike

我正在尝试执行一些查询来获取有关某些图像的信息页面。我写了一个函数

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}");
}

如果出现错误,应该抛出异常...

关于php - fatal error : Call to a member function fetch_assoc() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com