2013-08-19 54 views
-1

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

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

回答

3

尝试此相反的
if($result->num_rows < 1)

if($stmt->num_rows < 1)

您在结果对象得到NUM_ROWS

+1

你是对的感谢这部作品。 –

+0

很高兴工作:) – TGO

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

为什么投票? –

+0

试过它不起作用 –

+0

最新错误? –