如果没有与查询匹配的行,我希望显示"未找到结果"。i我试过了:
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'];}
试试这个if($result->num_rows < 1)
而不是if($stmt->num_rows < 1)
您在结果对象处获得num_rows
<?php if($stmt->num_rows != 0) {
while ($row = $result->fetch_assoc()) {
echo $row['message'];}
} else {echo"no results found";} ?>