PDO:调用非对象上的成员函数 fetch()?

发布于 2024-09-11 08:21:37 字数 689 浏览 4 评论 0原文

我只是在尝试 PDO,但收到此错误,致命错误:在非对象上调用成员函数 fetch(),但它不是已经在 $this->db 对象上了吗?

class shoutbox {

    private $db;

    function __construct($dbname, $username, $password, $host = "localhost" ) 
    { # db conections
        try {
            $this->db = new PDO("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    function getShouts()
    {
        $sql_shouts = $this->db->query('SELECT shoutid, message, pmuserid, ipadress, time FROM shouts WHERE pmuserid == 0');

        return $sql_shouts->fetch(PDO::FETCH_OBJ);

    }

}

I am just trying out PDO and I get this error, Fatal error: Call to a member function fetch() on a non-object, but isn't it already on the $this->db object?

class shoutbox {

    private $db;

    function __construct($dbname, $username, $password, $host = "localhost" ) 
    { # db conections
        try {
            $this->db = new PDO("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    function getShouts()
    {
        $sql_shouts = $this->db->query('SELECT shoutid, message, pmuserid, ipadress, time FROM shouts WHERE pmuserid == 0');

        return $sql_shouts->fetch(PDO::FETCH_OBJ);

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天冷不及心凉 2024-09-18 08:21:37

仔细查看 PDO::query 的文档,特别是“Return价值观”部分:

PDO::query() 返回 PDOStatement
对象,失败时为 FALSE。

重要的一点是“失败时为 FALSE”。 FALSE 不是对象,因此调用 ->fetch() 是个坏消息。

该错误可能是由于您使用“==”比较运算符造成的。在 SQL 中,它只是“=”。

您应该测试 $sql_shouts 是否为 false,然后对错误进行一些巧妙的处理(如果有错误):

<?PHP
$sql_shouts = $this->db->query('...');
if ($sql_shouts === false){
    $errorInfo = $this->db->errorInfo();
    //log the error or take some other smart action
}
return $sql_shouts->fetch(PDO::FETCH_OBJ);

Look carefully at the documentation for PDO::query, particularly the "Return Values" section:

PDO::query() returns a PDOStatement
object, or FALSE on failure.

The important bit is "FALSE on failure". FALSE is not an object, so calling ->fetch() is bad news.

The error is probably due to your use of "==" comparison operator. In SQL, it's just "=".

You should test that the $sql_shouts is not false, and then do something smart with the error, if there was one:

<?PHP
$sql_shouts = $this->db->query('...');
if ($sql_shouts === false){
    $errorInfo = $this->db->errorInfo();
    //log the error or take some other smart action
}
return $sql_shouts->fetch(PDO::FETCH_OBJ);
深空失忆 2024-09-18 08:21:37

我想说的是,由于语法不正确,您的查询未执行。您确实应该检查 PDO 的 errorinfo 静态函数以查看查询是否出错。

这是一个可能正确的 SQL 语句:

$sql_shouts = $this->db->query('SELECT shoutid, message, pmuserid, ipadress, `time` FROM shouts WHERE pmuserid = 0');

我相信 Time 是 MySQL 中的保留字,我建议使用不同的名称,但将其放在反引号中可以缓解该问题。 1个等号用于mysql,而不是两个。但是,是的,请查看 errorinfo 函数以确定您的查询是否由于语法错误而失败并妥善处理。

I would say that your query is not executing due to incorrect syntax. You should really check PDO's errorinfo static function to see if the query errored out or not.

Here is a potential correct SQL statement:

$sql_shouts = $this->db->query('SELECT shoutid, message, pmuserid, ipadress, `time` FROM shouts WHERE pmuserid = 0');

I believe Time is a reserved word in MySQL I would recommend using a different name but encasing it in backticks will alleviate that issue. 1 equal sign is used for mysql, not two. But yea, look into the errorinfo function to determine if your query failed due to a syntax error and handle it gracefully.

度的依靠╰つ 2024-09-18 08:21:37

当表也不存在时也会发生这种情况。确保它确实存在,而不仅仅是由于硬盘驱动器错误而保留在数据库中。

发生这种情况时,我建议您重新创建数据库/表。

It happens when the table doesn't exist also. make sure it actually exists, and isn't just a holder in the database due to hard drive errors.

When that happens I suggest you recreate the database/table.

翻身的咸鱼 2024-09-18 08:21:37

由于括号中的一个愚蠢错误,我收到了此错误消息。它嵌套在 if 语句中,只是没有看到它。

db_query("SELECT thing FROM table WHERE var=:var", array(":var" => $var)->fetchField());

我花了一段时间才发现我没有在正确的位置关闭 db_query 括号。也许它可以帮助其他人盯着这个疑惑。正确的:

db_query("SELECT thing FROM table WHERE var=:var", array(":var" => $var))->fetchField();

I got this error message due to a silly mistake with brackets. It was nested inside an if statement and just didn't see it.

db_query("SELECT thing FROM table WHERE var=:var", array(":var" => $var)->fetchField());

It took me a while to work out that I didn't close the db_query bracket in the right place. Maybe it helps someone else staring at this wondering wth. Correct:

db_query("SELECT thing FROM table WHERE var=:var", array(":var" => $var))->fetchField();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文