简体   繁体   English

调用资源上的成员函数fetch_object()

[英]Call to a member function fetch_object() on resource

I have written this PHP file which contains SQL to gather all posts in a table. 我已经写了这个PHP文件,其中包含SQL来收集表中的所有帖子。

$selectpost = mysql_query("
SELECT 
posts.id,
COUNT(post_likes.id) AS likes

FROM posts

LEFT JOIN post_likes
ON posts.id = post_likes.post

GROUP BY posts.id

");

while($row = $selectpost->fetch_object()) {

$posts[] = $row;    
}

for some reason i keep getting this error: 由于某些原因,我不断收到此错误:

Call to a member function fetch_object() on resource in posts.php on line 23 在第23行,在posts.php中的资源上调用成员函数fetch_object()

line 23 is the while loop: 第23行是while循环:

while($row = $selectpost->fetch_object()) {

connecting to database like this: 像这样连接到数据库:

$db = mysql_connect('localhost', 'account', 'password');

$dbs = mysql_select_db('site');

what is wrong, i cant fix it.. 有什么问题,我无法解决。

$selectpost->fetch_object() is a mysqli fucntion so $selectpost needs to be a mysqli result. $ selectpost-> fetch_object()是mysqli功能,因此$ selectpost必须是mysqli结果。

for mysql_query() uses the non object oriented mysql_fetch_object( ) 对于mysql_query(),使用非面向对象的mysql_fetch_object()

basically you mysql_query() hasn't returned a mysqli object but your trying to make mysqli methos calls on it. 基本上,您mysql_query()尚未返回mysqli对象,但您尝试对其进行mysqli方法调用。

http://php.net/manual/en/function.mysql-fetch-object.php http://php.net/manual/zh/function.mysql-fetch-object.php

You are using the mysql_ PHP extension so you would have to use 您正在使用mysql_ PHP扩展,因此您必须使用

while($row = mysql_fetch_object($selectpost)) {

Of course you should really not be using the mysql_ extension as it is deprecated 当然,您实际上不应该使用mysql_扩展名,因为它已被弃用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM