注意:尝试获取非对象的属性“ num_rows”

时间:2019-01-17 16:31:54

标签: php mysql

<?php

$queryuser  = "SELECT id, email, firstname, lastname, address, city, country  FROM users WHERE role = 'client'";
$resultuser = $connection->query($queryuser);
if ($resultuser->num_rows > 0)
{
  // output data of each row
  while ($rowuser = $resultuser->fetch_assoc())
  {
    $id_user   = $rowuser['id'];
    $firstname = $rowuser['firstname'];
    $lasttname = $rowuser['lastname'];
    $email     = $rowuser['email'];
    $phone     = $rowuser['phone'];
    $city      = $rowuser['city'];
    $state     = $rowuser['state'];
    $address   = $rowuser['address'];
?>

1 个答案:

答案 0 :(得分:0)

也许您已经从此页面https://www.w3schools.com/php/php_mysql_select.asp复制了代码

  • 请先逐行了解代码。您尚未在代码中定义$connection变量(可能是出于安全考虑的故意方法)。
  • 您的通知说,您正在尝试从对象num_rows获取$resultuser属性,但是在此查询中$resultuser不是对象,因为查询的任何错误/错误,因为查询()失败时返回FALSE,成功则返回对象。
  • 要确认,您可以使用var_dump();来检查结果集。
  • 如果 $ resultuser-> num_rows > 0替换为
    (!empty($ resultuser-> num_rows)&&($ resultuser-> num_rows> 0))),请替换此行/ li>