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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5121027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:52:13  来源:igfitidea点击:

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

phpmysqli

提问by nickles

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

that selects a page of these images based on their popularity. I've written a Databaseclass that handles interactions with the database and an Imageclass that holds information about an image. When I attempt to run this I get an error.

根据它们的受欢迎程度选择这些图像的页面。我编写了一个Database处理与数据库交互的Image类和一个保存图像信息的类。当我尝试运行它时,出现错误。

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

$resultis a mysqli resultset, so I'm baffled as to why this isn't working.

$result是一个 mysqli 结果集,所以我很困惑为什么这不起作用。

回答by ircmaxell

That's because there was an error in your query. MySQli->query()will return false on error. Change it to something like::

那是因为您的查询中有错误。 MySQli->query()出错时将返回 false。将其更改为类似::

$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...

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

回答by Marc B

Most likely your query failed, and the query call returned a boolean FALSE (or an error object of some sort), which you then try to use as if was a resultset object, causing the error. Try something like var_dump($result)to see what you really got.

很可能您的查询失败,并且查询调用返回一个布尔值 FALSE(或某种错误对象),然后您尝试将其用作结果集对象,从而导致错误。尝试类似的事情var_dump($result),看看你真正得到了什么。

Check for errors after EVERY database query call. Even if the query itself is syntactically valid, there's far too many reasons for it to fail anyways - checking for errors every time will save you a lot of grief at some point.

在每次数据库查询调用后检查错误。即使查询本身在语法上是有效的,它失败的原因也太多了 - 每次检查错误都会在某些时候为您节省很多麻烦。

回答by Upendra Bittu

I happen to miss spaces in my query and this error comes.

我碰巧在查询中遗漏了空格,并且出现了此错误。

Ex: $sql= "SELECT * FROM";
$sql .= "table1";

Though the example might look simple, when coding complex queries, the probability for this error is high. I was missing space before word "table1".

尽管该示例可能看起来很简单,但在编码复杂查询时,出现此错误的可能性很高。我在单词“table1”之前缺少空格。

回答by Mustafizur Rahman

Please check if you have already close the database connection or not. In my case i was getting the error because the connection was close in upper line.

请检查您是否已经关闭了数据库连接。在我的情况下,我收到错误,因为连接在上线关闭。