错误-尝试获取非对象的属性'num_rows' [英] Error - Trying to get property 'num_rows' of non-object

查看:73
本文介绍了错误-尝试获取非对象的属性'num_rows'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问为什么会出现此错误:

I want to ask why am I getting this error:

试图获取非对象的属性"num_rows"

Trying to get property 'num_rows' of non-object

具有以下代码:

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

我已经使用echo $sql来查看问题是否在那里,但是没有问题.我的SQL看起来不错.

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

我尝试使用Prepared Statements并遇到其他错误.

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

给出错误的行是带有if语句的那一行.

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

有什么想法吗?

推荐答案

这是遵循IMO的更好的(非错误检查)模式:

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)) {

您还应该考虑使用 PDO

这篇关于错误-尝试获取非对象的属性'num_rows'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆