找不到记录时不显示消息

时间:2017-09-28 21:13:59

标签: php arrays mysqli

出于某种原因,下面没有显示"没有找到结果"查询所选范围的数据库时出现错误消息,但显示表格页眉和页脚。

    $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);
?>

理论上,任何帮助都会受到极大的赞赏,这应该有用......难道不是吗? : - )

2 个答案:

答案 0 :(得分:3)

$result可能是结果集,但可能是空的。然而!$result不会是真的。 mysql_query的文档:

  

失败时返回FALSE。对于成功的SELECTSHOWDESCRIBEEXPLAIN个查询mysqli_query()将返回mysqli_result个对象。对于其他成功的查询,mysqli_query()将返回TRUE。 (来源:http://php.net/mysqli_query

您应该查看mysqli_num_rows(或类似的东西)

答案 1 :(得分: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);
?>