简体   繁体   English

PHP 试图获取非对象的属性

[英]PHP Trying to get property of a non-object

$sql = "select customer-name, customer-city from customer where customer-name in (select distinct customer-name from depositor, account where depositor.account-number=account.account-number and account.branch-name='Downtown')";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //LINE 18
  // output result in each row
  while($row = $result->fetch_assoc()) {
    echo "Name: ". $row['customer-name']. " " . $row["customer-city"] . "<br>";
  }
} else {
  echo "0 results";
}

The error I get:我得到的错误:

PHP Notice:  Trying to get property of non-object in line 18

I tried using an array and that did not work either, I got the same error.我尝试使用数组,但也没有用,我得到了同样的错误。

In the PDO call of query ( https://www.php.net/manual/en/pdo.query.php ), you can see the following: PDO::query() returns a PDOStatement object, or false on failure. In the PDO call of query ( https://www.php.net/manual/en/pdo.query.php ), you can see the following: PDO::query() returns a PDOStatement object, or false on failure.

It means that if the query fails, you keep only a false instead of the PDOStatement object.这意味着如果查询失败,您只保留一个false而不是PDOStatement object。

$sql = "select customer-name, customer-city from customer where customer-name in (select distinct customer-name from depositor, account where depositor.account-number=account.account-number and account.branch-name='Downtown')";
$result = $conn->query($sql);
if ($result == false) {
  echo "Query failed";
} else if ($result->num_rows > 0) {
  // output result in each row
  while($row = $result->fetch_assoc()) {
    echo "Name: ". $row['customer-name']. " " . $row["customer-city"] . "<br>";
  }
} else {
  echo "0 results";
}

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

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