mysqli显示“找不到结果”

时间:2013-08-19 04:47:50

标签: php mysqli

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

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'];}

2 个答案:

答案 0 :(得分:3)

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

您在结果对象

处获得num_rows

答案 1 :(得分:-1)

<?php if($stmt->num_rows != 0) {
while ($row = $result->fetch_assoc()) {
echo $row['message'];}
} else {echo"no results found";} ?>
相关问题