Téléverser les fichiers vers "sql"

This commit is contained in:
kilian.te 2025-05-16 09:28:38 +00:00
parent 48c6cf4022
commit 30dd67df75

26
sql/sql_utils.php Normal file
View File

@ -0,0 +1,26 @@
<?php
function get_books($page = 1, $resultsPerPage = 10){
global $pdo;
$page = max(1, (int)$page);
$offset = ($page - 1) * $resultsPerPage;
$count_query = 'SELECT COUNT(*) FROM Livres AS L INNER JOIN Auteurs AS A ON L.auteur_id = A.auteur_id';
$count = $pdo->query($count_query)->fetchColumn();
$total_pages = ceil($count / $resultsPerPage);
$statement = $pdo->prepare('SELECT L.titre, L.annee_publication, A.nom, A.prenom
FROM Livres AS L
INNER JOIN Auteurs AS A ON L.auteur_id = A.auteur_id
LIMIT :limit OFFSET :offset');
$statement->bindValue(':limit', $resultsPerPage, PDO::PARAM_INT);
$statement->bindValue(':offset', $offset, PDO::PARAM_INT);
$statement->execute();
return [
'books' => $statement->fetchAll(PDO::FETCH_ASSOC),
'current_page' => $page,
'total_pages' => $total_pages,
'total_results' => $count
];
}