// 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>");
}
I'm trying to echo "No results to display" if my MySQLi result returns empty. The problem is the headers are returned as part of the array, how can I achieve this?
You can use num_rows
to get the total value of records retrieved from the query.
$row_cnt = $result->num_rows;
if($row_cnt>0){ echo "Have data";}else{echo "No data";}
You need to check with number of rows. if it return zero it means table contain no rows. try below code.
// 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>");
}
}
Simple use num_rows
if($result->num_rows== 0){
echo "no results to display"
}
else{
while ($myvar = $result->fetch_row()) {
//Your Code here
}
You can use num_rows
to get the total value of records.
$row_count = $result->num_rows;
if($row_count>0)
{
echo "Data Available";
}
else
{
echo "No Data Available";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.