2017-09-28 76 views
1

由於某種原因,當查詢數據庫的選定範圍時,下面沒有顯示「找不到結果」錯誤消息,但正在顯示錶頭和頁腳。當沒有找到記錄時不顯示消息

$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC"); 

<?php 

if (!$result) { echo("No results found for the selected view"); 
      } else ?> 
      <table id="results"> 
<tr> 
<th>Rec#</th> 
<th>Date</th> 
<th>Time</th> 
<th>Reading</th> 
</tr> 
<?php ; 
while($row = mysqli_fetch_array($result)) 
?> 
<tr> 

<td><?php echo($row['RecID']);?></td> 
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td> 
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td> 
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td> 
</tr> 
<?php 
} 
?> 
<tr> 
<td class="footer" colspan="4">- end of report -</td></tr> 
</table> 
<?php 
mysqli_close($con); 
?> 

任何援助將不勝感激理論上,這應該工作......不應該嗎? :-)

+0

它可能會有所幫助看看你是如何創建'$ result' –

+0

True @PatrickQ ...我已更新顯示原點 – cloudseeker

回答

1

你可以用這個條件來檢查查詢行號檢查

$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC"); 

<?php 

if (mysqli_num_rows($result) === 0) { 
    echo("No results found for the selected view"); 
} else {?> 
<table id="results"> 
<tr> 
<th>Rec#</th> 
<th>Date</th> 
<th>Time</th> 
<th>Reading</th> 
</tr> 
<?php ; 
while($row = mysqli_fetch_array($result)) 
?> 
<tr> 

<td><?php echo($row['RecID']);?></td> 
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td> 
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td> 
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td> 
</tr> 
<?php } ?> 
<tr> 
<td class="footer" colspan="4">- end of report -</td></tr> 
</table> 
<?php 
mysqli_close($con); 
?> 
+0

這是否有任何優勢或劣勢vs'if(mysqli_num_rows($ result)<1){echo(「沒有找到所選視圖的結果」);或者這是另一種方式來做一樣? :-) – cloudseeker

+0

沒有兩者都是相同的結果永遠不會消極和最小數量總是0,所以我更喜歡這一點。 –

+0

感謝您的澄清:-) – cloudseeker

3

$result可能是一個結果集,但它可能是空的。但!$result將不會成立。 mysql_query的文檔:

返回FALSE失敗。爲成功SELECT,SHOW,DESCRIBEEXPLAIN查詢mysqli_query()將返回一個mysqli_result對象。對於其他成功的查詢mysqli_query()將返回TRUE。 (來源:http://php.net/mysqli_query

你應該mysqli_num_rows(或類似的東西)

+0

正確...爲if語句我將其替換爲'if(mysqli_num_rows($ result)<1){echo( 「沒有找到所選視圖的結果」);'現在按原樣運行:-)謝謝@jakumi – cloudseeker

相關問題