当记录不存在时如何添加消息....?



这是我的HTML PHP表页的完整代码。这是有效的,但当记录不存在时,它不会显示任何内容。所以我想在记录不存在时显示消息。

<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<td>Subjects</td>
<td>Grades</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($results_teach)) {
if($row["Sub_1"] != "" && $row["Sub_Grades_1"] != "") { 
?>
<tr>
<td><?php echo $row["Sub_1"]; ?></td>
<td><?php echo $row["Sub_Grades_1"]; ?></td>
</tr>
<?php
}
$i++;
}
?>
</table>
</body>
</html> 

您有一个名为$i的计数器。你可以简单地检查它的值:

<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<td>Subjects</td>
<td>Grades</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($results_teach)) {
if($row["Sub_1"] != "" && $row["Sub_Grades_1"] != "") { 
?>
<tr>
<td><?php echo $row["Sub_1"]; ?></td>
<td><?php echo $row["Sub_Grades_1"]; ?></td>
</tr>
<?php
}
$i++;
}
?>
</table>
<?php
if ($i === 0) {
?>
Does not exist
<?php
}
?>
</body>
</html> 

您可以通过mysqli_num_rows函数检查提取数据的计数。如果没有数据被提取,计数将为零,您可以显示错误消息

if(mysqli_num_rows($results_teach) < 0){
echo "The error message"
}else{
$i=0;
//code
}