What are common pitfalls when implementing pagination in PHP for database queries?

Common pitfalls when implementing pagination in PHP for database queries include not properly calculating the OFFSET value for each page, not handling the total number of pages correctly, and not sanitizing user input to prevent SQL injection attacks.

// Calculate the OFFSET value for pagination
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Query to fetch data with pagination
$sql = "SELECT * FROM table_name LIMIT $limit OFFSET $offset";
$result = mysqli_query($conn, $sql);

// Calculate total number of pages
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $limit);

// Sanitize user input to prevent SQL injection
$page = filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT);