19

I am using this code, but I don't understand how to check if the query returns zero rows. How can I check that?

$results = $mysqli->query("SELECT ANNOUNCE_NUMBER,ANNOUNCEMENTS,ANNOUNCE_TYPE,POST_DATE FROM home ORDER BY ANNOUNCE_NUMBER DESC");

while ($obj = $results->fetch_object()) {
    //output results from database
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
user3196424
  • 537
  • 3
  • 8
  • 16

1 Answers1

37

You can use the num_rows on the dataset to check the number of rows returned. Example:

$results = $mysqli->query("SELECT ANNOUNCE_NUMBER,ANNOUNCEMENTS,ANNOUNCE_TYPE,POST_DATE FROM home ORDER BY ANNOUNCE_NUMBER DESC");

if ($results->num_rows === 0) {
    echo 'No results';
} else {
    while ($obj = $results->fetch_object()) {
        //output results from database
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
MacMac
  • 34,294
  • 55
  • 151
  • 222