What are some potential pitfalls of generating HTML pages with a large number of database entries in PHP?
One potential pitfall of generating HTML pages with a large number of database entries in PHP is the risk of running out of memory due to loading and processing a large amount of data at once. To solve this issue, you can paginate the database results and only load a subset of data at a time to reduce memory usage.
// Example of paginating database results in PHP
$limit = 10; // Number of entries to display per page
$page = isset($_GET['page']) ? $_GET['page'] : 1; // Current page number
$offset = ($page - 1) * $limit; // Calculate the offset for the query
// Query to fetch data with pagination
$query = "SELECT * FROM table_name LIMIT $limit OFFSET $offset";
$result = mysqli_query($connection, $query);
// Loop through the results and display them in HTML
while($row = mysqli_fetch_assoc($result)) {
// Display data in HTML
}
// Pagination links
$total_pages = ceil($total_entries / $limit);
for($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}