What are some common pitfalls that PHP beginners may encounter when trying to implement pagination for displaying a fixed number of entries per page?

One common pitfall is not properly calculating the total number of pages based on the total number of entries and the desired number of entries per page. Another pitfall is not correctly passing the page number parameter in the URL to navigate to different pages. Additionally, beginners may forget to handle cases where the page number is out of bounds, leading to errors or displaying incorrect data.

// Calculate the total number of pages
$totalPages = ceil($totalEntries / $entriesPerPage);

// Get the current page number from the URL parameter
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// Make sure the page number is within bounds
if($page < 1) {
    $page = 1;
} elseif($page > $totalPages) {
    $page = $totalPages;
}

// Calculate the offset for the SQL query
$offset = ($page - 1) * $entriesPerPage;

// Query the database with the calculated offset and limit
$query = "SELECT * FROM entries LIMIT $offset, $entriesPerPage";
$result = mysqli_query($connection, $query);