出于某种原因,下面没有显示"没有找到结果"查询所选范围的数据库时出现错误消息,但显示表格页眉和页脚。
$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 :(得分:3)
$result
可能是结果集,但可能是空的。然而!$result
不会是真的。 mysql_query
的文档:
失败时返回
FALSE
。对于成功的SELECT
,SHOW
,DESCRIBE
或EXPLAIN
个查询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);
?>