对非对象的成员函数execute()的调用


Call to a member function execute() on a non-object

我在这个网站上很忙,我需要从数据库中获取工作人员发送的"tweets"。所以,我想,好吧,让我们连接到数据库,看看会发生什么。这就是代码(直到执行部分,之后你就不需要它了。)

$connection = new mysqli('localhost', 'root', '123', 'gh24hh');
$stmt = $connection->prepare('SELECT * FROM tweet ORDER BY id DESC LIMIT 5');
$stmt->execute();

这就是抛出的错误:

Fatal error:  Call to a member function execute() on a non-object in C:'xampp'htdocs'twitter.php on line 5

显然,第五行是执行行。

有人看到我的错误了吗?

(如果需要更多的代码,请这样说,因为我真的不知道该发布什么问题)

试试这个:

$connection = new mysqli('localhost', 'root', '123', 'gh24hh');
$result = $connection->query('SELECT * FROM tweet ORDER BY id DESC LIMIT 5')

由于变量$stmt返回false,因此引发错误。对于$stmt返回您需要更正查询的内容

正如@joe在评论it returns the statement object, or false if something went wrong中指出的那样。它不会返回null。