How can PHP be used to handle empty query results from a database fetch operation?

When fetching data from a database in PHP, it is important to handle cases where the query may return no results. One way to handle empty query results is to check if the fetch operation returned false, indicating that there are no rows to fetch. In this case, you can display a message to the user or perform any other necessary action.

// Perform database query
$query = "SELECT * FROM table WHERE condition";
$result = mysqli_query($connection, $query);

// Check if there are results
if(mysqli_num_rows($result) > 0) {
    // Fetch and display results
    while($row = mysqli_fetch_assoc($result)) {
        // Process fetched data
    }
} else {
    // Display a message for empty results
    echo "No results found.";
}