What best practices should be followed when displaying database records in a tabular format using PHP?

When displaying database records in a tabular format using PHP, it is important to sanitize the data to prevent SQL injection attacks. Additionally, it is recommended to use prepared statements to securely interact with the database. Lastly, consider implementing pagination for large datasets to improve performance and user experience.

<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Prepare and execute a query to retrieve data
$stmt = $pdo->prepare("SELECT * FROM table");
$stmt->execute();
$records = $stmt->fetchAll();

// Display the data in a table format
echo '<table>';
echo '<tr><th>ID</th><th>Name</th><th>Email</th></tr>';
foreach ($records as $record) {
    echo '<tr>';
    echo '<td>' . $record['id'] . '</td>';
    echo '<td>' . $record['name'] . '</td>';
    echo '<td>' . $record['email'] . '</td>';
    echo '</tr>';
}
echo '</table>';
?>