What potential pitfalls should be avoided when handling pagination in PHP for news articles?

When handling pagination for news articles in PHP, it is important to avoid pitfalls such as not properly sanitizing user input, not validating input data, and not handling errors gracefully. To ensure a secure and efficient pagination system, always validate and sanitize user input, handle errors effectively, and implement proper error handling techniques.

// Example PHP code snippet for handling pagination in news articles

// Sanitize and validate user input for page number
$page = isset($_GET['page']) ? filter_var($_GET['page'], FILTER_VALIDATE_INT) : 1;

// Set pagination limit and offset
$limit = 10;
$offset = ($page - 1) * $limit;

// Query database for news articles with pagination
$query = "SELECT * FROM news_articles LIMIT $limit OFFSET $offset";
$result = mysqli_query($connection, $query);

// Handle errors gracefully
if (!$result) {
    die("Error querying database: " . mysqli_error($connection));
}

// Display news articles
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['title'] . "<br>";
    echo $row['content'] . "<br>";
}

// Display pagination links
// code for pagination links goes here