check if the query returns zero rows [duplicate]

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
    }
}

Leave a Comment