如果查询无法找到任何结果,我如何让PHP显示一条消息,表明它无法找到任何结果,而不会在结果中显示任何结果?
以下是我的代码以显示结果:
print " <table border = '1'> \n";
print " <tr> \n";
while ($field = mysqli_fetch_field($result)){
print " <th>$field->name</th> \n";
} // end while
print " </tr> \n";
while ($row = mysqli_fetch_assoc($result)){
print " <tr> \n";
foreach ($row as $name => $value){
print " <td>$value</td> \n";
} // end foreach
print " </tr> \n";
} // end while loop
print " </table> \n";
答案 0 :(得分:1)
通过mysqli_num_rows(),您可以计算表中包含数据的行数。
$row_cnt = mysqli_num_rows($result);
if($row_cnt > 0){
//data exist for it
}else{
//Display your error message here
}