在这里,我做了一个项目,当我搜索日期的时候。如果没有结果,我想输出no data found。但在这里,如果没有结果,我会得到表格式。如果数据存在,它就没问题!但问题是当数据库中没有数据时。
<?php
include('connect.php');
?>
<?php
// echo $res;
$result = mysqli_query($con,"SELECT * FROM buyer where date='$res'");
//$result = mysqli_query($con, "SELECT * FROM buyer WHERE date='09-01-14'");
if($result==NULL)
{
echo "no data found";
}
else{
echo "<table class='CSSTable'>
<tr>
<th>invoice no</th>
<th>Buyer Name</th>
<th>Buyer Order number</th>
<th>Date</th>
<th>Total Amount</th>
<th>Total Items</th>
<th>Generate PDF</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['invoice_number'] . "</td>";
echo "<td>" . $row['buyer_name'] . "</td>";
echo "<td>" . $row['buyer_order_number'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['total_amount'] . "</td>";
echo "<td>" . $row['total_items'] . "</td>";
echo "<td>" ." <a href='get_pdf.php?id={$row['invoice_number']}' target='_blank'><img src='image/download.png' width='16' height='16' /></a>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>发布于 2014-02-12 20:19:45
发布于 2014-02-12 20:24:53
我假设您使用如下所示的代码行调用了mysqli_query:
$result = mysqli_query($query);如果在执行查询false时失败,mysqli_query将仅返回。如果查询成功执行并且没有选择任何行,则不会返回false (本例中可能就是这样)。您可以参考mysqli_query found here上的文档。
相反,您要做的是确定成功执行的查询的$result是否有任何数据行。为此,您可以调用mysqli_num_rows($result)。这将返回查询返回的行数。如果它等于0,那么这就是您想要处理的“无数据”情况。
发布于 2014-02-12 20:25:00
尝试使用mysqli_num_rows()检查返回的行数
$num = mysqli_num_rows($result);
if($num == 0)
{
echo "no data found";
}
else
{
//do something
}https://stackoverflow.com/questions/21727725
复制相似问题