What best practices should be followed when handling empty database results in PHP scripts to ensure proper output formatting?

When handling empty database results in PHP scripts, it is important to check if there are any results before attempting to format or display them. This can prevent errors or unexpected behavior in the output. One way to handle this is by using conditional statements to check if there are any results before attempting to loop through them for formatting.

// Check if there are any results before formatting and displaying them
if($results) {
    foreach($results as $result) {
        // Format and display the results here
    }
} else {
    echo "No results found.";
}