我正在尝试检查SQL中是否存在记录,并返回->True或False。它返回一个通知:注意:正在尝试获取…中非对象的属性。if($result->num_rows>0)
$connection = new mysqli('localhost', 'user', 'pass','db_name');
$query = "SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$result = $connection->query($query);
if($result->num_rows > 0) {
echo 'found'; // The record(s) do exist
}
else{
echo 'not found'; // No record found
}
$connection->close();
ORDER
是一个mysql保留字。使用它作为字段名、表名或除ORDER BY fieldname
之外的任何名称需要使用backtick:
SELECT * FROM `order` WHERE ....
否则,您将得到一个查询错误,它将把$result
变成布尔值false。
此外,您的代码容易受到sql注入的攻击。我建议您使用事先准备好的报表。
您正在检查查询对象的行。查询对象不包含行。
你想做的是类似的事情
$data = $result->fetch_array();
if ($data->num_rows > 0)
{
//Rows exist
} else {
//No rows exist
}
如果查询没有在$result
变量中注入对象,就会发生这种情况。您可以进行以下更改以继续。
if(is_object($result) && $result->num_rows > 0) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
或
if(!empty($result->num_rows)) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
也要保持简单:
$query = "SELECT * FROM `order` WHERE telephone = '$telephone' AND order_status_id='0' ";
Use like this
$sqlReq = " SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$queryReq = mysql_query($sqlReq) or die(mysql_error());
if(mysql_num_rows($queryReq) > 0)
{
echo "found";
}else{
echo "not found";
}