// Populate headers
$fields = $result->fetch_fields();
foreach ($fields as $field)
printf("<th>%s</th>", $field->name);
printf("</tr>");
// Write to table
while ($myvar = $result->fetch_row()) {
$date = $myvar[0];
$room_ID = $myvar[1];
$description = $myvar[2];
$firstname = $myvar[3];
$lastname = $myvar[4];
$message = $myvar[5];
$period = $myvar[6];
printf("<tr>");
printf("<td>%s</td><td>%s</td>", $date, $room_ID);
printf("<td>%s</td><td>%s</td>", $description, $firstname);
printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period);
printf("</tr>");
}
如果我的MySQLi结果返回空,我正在尝试回显“没有结果显示”。问题是标题是作为数组的一部分返回的,我该如何实现呢?
答案 0 :(得分:1)
您可以使用 num_rows
来获取从查询中检索到的记录的总值。
$row_cnt = $result->num_rows;
if($row_cnt>0){ echo "Have data";}else{echo "No data";}
答案 1 :(得分:1)
您需要检查行数。如果它返回零,则表示表不包含任何行。尝试以下代码。
// Populate headers
$fields = $result->fetch_fields();
foreach ($fields as $field)
printf("<th>%s</th>", $field->name);
printf("</tr>");
// Write to table
if($result->num_rows== 0){
echo "No Result to display";
}else{
while ($myvar = $result->fetch_row()) {
$date = $myvar[0];
$room_ID = $myvar[1];
$description = $myvar[2];
$firstname = $myvar[3];
$lastname = $myvar[4];
$message = $myvar[5];
$period = $myvar[6];
printf("<tr>");
printf("<td>%s</td><td>%s</td>", $date, $room_ID);
printf("<td>%s</td><td>%s</td>", $description, $firstname);
printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period);
printf("</tr>");
}
}
答案 2 :(得分:1)
简单使用num_rows
if($result->num_rows== 0){
echo "no results to display"
}
else{
while ($myvar = $result->fetch_row()) {
//Your Code here
}
答案 3 :(得分:1)
您可以使用num_rows
来获取记录的总值。
$row_count = $result->num_rows;
if($row_count>0)
{
echo "Data Available";
}
else
{
echo "No Data Available";
}