What are the best practices for creating a pagination function that is database-independent in PHP?

When creating a pagination function that is database-independent in PHP, it is important to abstract the database queries and use a parameterized approach to ensure compatibility with different database systems. By separating the pagination logic from the database queries, you can easily switch between databases without having to rewrite the pagination function.

function getPaginatedData($page, $limit) {
    $offset = ($page - 1) * $limit;
    
    // Perform database query using placeholders for pagination
    $query = "SELECT * FROM table_name LIMIT :limit OFFSET :offset";
    
    // Execute the query with parameters
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// Example usage
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$data = getPaginatedData($page, $limit);