How can the code for displaying products from a database be improved in terms of efficiency and readability?

The code for displaying products from a database can be improved by using prepared statements to prevent SQL injection attacks, separating HTML markup from PHP logic for better readability, and implementing pagination to limit the number of products displayed per page for efficiency.

// Improved code for displaying products from a database with prepared statements and pagination

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

// Pagination variables
$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $limit;

// Prepare SQL query with pagination
$stmt = $pdo->prepare("SELECT * FROM products LIMIT :start, :limit");
$stmt->bindParam(':start', $start, PDO::PARAM_INT);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();

// Display products
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<div class="product">';
    echo '<h3>' . $row['name'] . '</h3>';
    echo '<p>' . $row['description'] . '</p>';
    echo '<p>$' . $row['price'] . '</p>';
    echo '</div>';
}

// Pagination links
$total_pages = ceil($pdo->query("SELECT COUNT(*) FROM products")->fetchColumn() / $limit);
for ($i = 1; $i <= $total_pages; $i++) {
    echo '<a href="?page=' . $i . '">' . $i . '</a> ';
}