PDO:调用非对象上的成员函数 fetch()?
我只是在尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仔细查看 PDO::query 的文档,特别是“Return价值观”部分:
重要的一点是“失败时为 FALSE”。 FALSE 不是对象,因此调用 ->fetch() 是个坏消息。
该错误可能是由于您使用“==”比较运算符造成的。在 SQL 中,它只是“=”。
您应该测试
$sql_shouts
是否为 false,然后对错误进行一些巧妙的处理(如果有错误):Look carefully at the documentation for PDO::query, particularly the "Return Values" section:
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:我想说的是,由于语法不正确,您的查询未执行。您确实应该检查 PDO 的 errorinfo 静态函数以查看查询是否出错。
这是一个可能正确的 SQL 语句:
我相信 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:
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.
当表也不存在时也会发生这种情况。确保它确实存在,而不仅仅是由于硬盘驱动器错误而保留在数据库中。
发生这种情况时,我建议您重新创建数据库/表。
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.
由于括号中的一个愚蠢错误,我收到了此错误消息。它嵌套在 if 语句中,只是没有看到它。
我花了一段时间才发现我没有在正确的位置关闭 db_query 括号。也许它可以帮助其他人盯着这个疑惑。正确的:
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.
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: