-1

如果没有与查询匹配的行,我想显示“未找到结果”。我试过:

if(!$result) {echo"no results found";} 

if($stmt->num_rows < 1) {echo"no results found"}

但它们都不起作用。什么是正确的程序?

 $stmt = $mydb->prepare("SELECT * FROM messages where from_user = ? and deleted = 'yes' or to_user  = ? and deleted = 'yes'");
 $stmt->bind_param('ss', $username->username, $username->username);
 $stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
echo $row['message'];}
4

2 回答 2

3

试试这个
if($result->num_rows < 1)
而不是
if($stmt->num_rows < 1)

您在结果对象中获得 num_rows

于 2013-08-19T04:54:45.800 回答
-1
<?php if($stmt->num_rows != 0) {
while ($row = $result->fetch_assoc()) {
echo $row['message'];}
} else {echo"no results found";} ?>
于 2013-08-19T04:54:49.047 回答