2013-08-19 81 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

最新錯誤? –