test
This commit is contained in:
57
sql_utilis.php
Normal file
57
sql_utilis.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
include("pdo.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.livre_id, 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
|
||||
];
|
||||
}
|
||||
|
||||
function get_book($idBook = null) {
|
||||
global $pdo;
|
||||
|
||||
if (is_numeric($idBook)) {
|
||||
$statement = $pdo->prepare('SELECT L.titre, L.annee_publication, L.resume, A.nom, A.prenom, A.auteur_id
|
||||
FROM Livres AS L
|
||||
INNER JOIN Auteurs AS A ON L.auteur_id = A.auteur_id
|
||||
WHERE L.livre_id = :id');
|
||||
|
||||
$statement->bindValue(':id', $idBook, PDO::PARAM_INT);
|
||||
$statement->execute();
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
|
||||
function get_author($idAuthor = null) {
|
||||
global $pdo;
|
||||
|
||||
if (is_numeric($idAuthor)) {
|
||||
$statement = $pdo->prepare('SELECT A.nom, A.prenom, L.titre, L.livre_id
|
||||
FROM Auteurs AS A
|
||||
INNER JOIN Livres AS L ON L.auteur_id = A.auteur_id
|
||||
WHERE A.auteur_id = :id');
|
||||
$statement->bindValue(':id', $idAuthor, PDO::PARAM_INT);
|
||||
$statement->execute();
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user