What are the best practices for handling and formatting database results for display in PHP?

When displaying database results in PHP, it's important to properly handle and format the data to ensure it is presented correctly to the user. One common practice is to loop through the results and extract the necessary information, then format it using HTML or CSS for display on the webpage. Additionally, using functions like htmlentities() can help prevent security vulnerabilities such as SQL injection attacks.

// Assuming $results is an array of database results
foreach ($results as $result) {
    $name = htmlentities($result['name']);
    $age = $result['age'];
    
    echo "<p>Name: $name</p>";
    echo "<p>Age: $age</p>";
}