Error - Trying to get property 'num_rows' of non-object [duplicate]

这一生的挚爱 提交于 2020-01-06 06:24:51

问题


I want to ask why am I getting this error:

Trying to get property 'num_rows' of non-object

with the following code:

$sql = "SELECT * FROM $table_name ORDER BY Author";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {...

I have used echo $sql to look if the problem lies there, but there is no problem. My SQL looks fine.

I have tried to use Prepared Statements and got other errors too.

The line which gives the error is the one with the if statement.

Any ideas?


回答1:


This is a better (non-error checking) pattern to follow IMO:

$sql = "SELECT * FROM $table_name ORDER BY Author";
$result = $mysqli->query($sql);
if (!empty($result)) {

You should also consider using PDO



来源:https://stackoverflow.com/questions/50975752/error-trying-to-get-property-num-rows-of-non-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!