What are best practices for handling empty or null results from MySQL queries in PHP to prevent displaying blank pages or incomplete content?

When querying a MySQL database in PHP, it is important to handle cases where the result set may be empty or null to prevent displaying blank pages or incomplete content to users. One way to address this is by checking if the result set is empty before attempting to display the data. This can be done using conditional statements to handle such scenarios gracefully.

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

// Check if the result set is empty
if(mysqli_num_rows($result) > 0) {
    // Fetch and display data
    while($row = mysqli_fetch_assoc($result)) {
        // Display data here
    }
} else {
    // Handle empty result set
    echo "No results found.";
}