2011-01-31 104 views
0

這應該很簡單....但它需要一段時間...以下是不工作的代碼(它不顯示任何內容或每次都顯示空白狀態消息)。 $顯示圖像是查詢,我知道它運行良好。PHP MYQSLi如果字段爲空,返回一個不同的值

// BLANK STATE TOGGLE 
$result = mysqli_fetch_array($showimage, MYSQLI_ASSOC); 
if($result == ''){ 
     echo '<p>Sorry- no image.</p>'; 
} 
else { 
    echo '<p>There is an image!</p>'; 
    } 
} 
+0

你怎麼知道它運行良好?因爲`$ result`可以是```````false`的唯一場景是查詢失敗 – 2011-01-31 03:43:42

+0

`$ showimage`是一個mysqli_result嗎?你有錯誤報告啓用和`display_errors`設置爲「開」?此外,來自`mysqli_fetch_array`的返回值是一個數組或`NULL`。您可能應該針對其中的一個而不是空字符串進行測試。 – Phil 2011-01-31 03:44:26

+0

好的 - 他們如何檢查空表值? – WillHerndon 2011-01-31 03:46:02

回答

0

如果你只是要檢查行的結果存在從您的查詢,爲什麼不你是這樣簡化它

// $db is your MySQLi connection object 

$query = 'SELECT COUNT(1) FROM `table` WHERE `something` = ?'; 
$stmt = $db->prepare($query); 
$stmt->bind_param('s', $something); 
$stmt->execute(); 

$stmt->bind_result($rowCount); 
$stmt->fetch(); 
$stmt->close(); 

if ($rowCount > 0) : ?> 
<p>There is an image!</p> 
<?php else : ?> 
<p>Sorry- no image.</p> 
<?php endif ?> 
-1

mysqli_fetch_array如果數據庫中沒有匹配,則返回null。所以你需要檢查null。

您可能需要試試這個:

如果$showimage是您的查詢..

//This should run fine 
//$link is ur connection 
$new_result = mysqli_query($link,$showimage); 


$result = mysqli_fetch_array($new_result, MYSQLI_ASSOC); 
if($result == null){ 
     echo '<p>Sorry- no image.</p>'; 
} 
else { 
    echo '<p>There is an image!</p>'; 
    } 
} 
相關問題