What are the potential pitfalls of using pagination for displaying posts in PHP?

One potential pitfall of using pagination for displaying posts in PHP is inefficient database queries, as fetching a large number of posts at once can slow down the page load time. To solve this issue, we can limit the number of posts fetched from the database using SQL's LIMIT clause and offset the results based on the current page number.

// Assuming $currentPage is the current page number and $postsPerPage is the number of posts to display per page

$offset = ($currentPage - 1) * $postsPerPage;
$query = "SELECT * FROM posts LIMIT $offset, $postsPerPage";
$result = mysqli_query($conn, $query);

// Display posts fetched from the database
while($row = mysqli_fetch_assoc($result)) {
    // Display post content here
}