php PDO:在非对象上调用成员函数 fetch()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3349612/
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
PDO: Call to a member function fetch() on a non-object?
提问by Carlos
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?
我只是在尝试 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);
}
}
回答by timdev
Look carefully at the documentation for PDO::query, particularly the "Return Values" section:
仔细查看PDO::query的文档,特别是“返回值”部分:
PDO::query() returns a PDOStatement object, or FALSE on failure.
PDO::query() 返回一个 PDOStatement 对象,或者失败时返回 FALSE。
The important bit is "FALSE on failure". FALSE is not an object, so calling ->fetch() is bad news.
重要的一点是“失败时错误”。FALSE 不是一个对象,所以调用 ->fetch() 是个坏消息。
The error is probably due to your use of "==" comparison operator. In SQL, it's just "=".
该错误可能是由于您使用了“==”比较运算符。在 SQL 中,它只是“=”。
You should test that the $sql_shoutsis not false, and then do something smart with the error, if there was one:
您应该测试$sql_shouts是不是错误,然后对错误做一些聪明的事情,如果有的话:
<?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);
回答by Jim W.
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.
我会说您的查询由于不正确的语法而没有执行。您真的应该检查 PDO 的 errorinfo 静态函数以查看查询是否出错。
Here is a potential correct SQL statement:
这是一个潜在的正确 SQL 语句:
$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.
我相信 Time 是 MySQL 中的保留字,我建议使用不同的名称,但将其括在反引号中将缓解该问题。1个等号用于mysql,而不是两个。但是,是的,查看 errorinfo 函数以确定您的查询是否由于语法错误而失败并优雅地处理它。
回答by a coder
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.
发生这种情况时,我建议您重新创建数据库/表。
回答by Paul
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.
由于括号的愚蠢错误,我收到了此错误消息。它嵌套在 if 语句中,只是没有看到它。
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 括号。也许它可以帮助其他人盯着这个疑惑。正确的:
db_query("SELECT thing FROM table WHERE var=:var", array(":var" => $var))->fetchField();

