1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
<?php
require_once 'DatabaseConnection.php';
class Albums
{
private $_db;
private $_sql;
private $_statement;
public function __construct()
{
$this->_db = DatabaseConnection::getInstance()->handle();
}
public function __destruct()
{
var_dump($this->_db); // Vérification
}
public function addAlbum($artist, $title)
{
try {
$this->_sql = "INSERT INTO albums (artist, title) VALUES (:artist, :title)";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':artist', $artist);
$this->_statement->bindParam(':title', $title);
$this->_statement->execute();
echo 'Album added.';
} catch (PDOException $e) {
die('Error->addAlbum() : ' . $e->getMessage());
}
}
public function editAlbum($id, $artist, $title)
{
try {
$this->_sql = "UPDATE albums SET artist = :artist, title = :title WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
$this->_statement->bindParam(':artist', $artist);
$this->_statement->bindParam(':title', $title);
$this->_statement->execute();
echo 'Album information updated.';
} catch (PDOException $e) {
die('Error->editAlbum() : ' . $e->getMessage());
}
}
public function deleteAlbum($id)
{
try {
$this->_sql = "DELETE FROM albums WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
$this->_statement->execute();
echo 'Album deleted.';
} catch (PDOException $e) {
die('Error->deleteAlbum() : ' . $e->getMessage());
}
}
public function selectAlbum($id = null)
{
try {
if ($id !== null && $id > 0) {
$this->_sql = "SELECT id, artist, title FROM albums WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
} else {
$this->_sql = "SELECT id, artist, title FROM albums";
$this->_statement = $this->_db->prepare($this->_sql);
}
$this->_statement->setFetchMode(PDO::FETCH_ASSOC);
$this->_statement->execute();
return $this->_statement->fetchAll();
} catch (PDOException $e) {
die('Error->selectAlbum() : ' . $e->getMessage());
}
}
} |
Partager