host = $host; $this->user = $user; $this->pass = $pass; $this->dbname = $dbname; } public function connect() { $driver_options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]; $dsn = "mysql:host=$this->host;dbname=$this->dbname"; $this->pdo = new PDO($dsn, $this->user, $this->pass,$driver_options); } public function query($sql, $params = []) { $stmt = $this->pdo->prepare($sql); if ($stmt->execute($params) === true) return $stmt; return false; } public function lastInsertId(){ return $this->pdo->lastInsertId(); } } class MusicAPI { static private $db = null; static private $BASE_QUERY = "SELECT Song.id, Song.name, Album.name AS `album-name`, Album.year, Artist.name AS `artist-name`, Genre.name AS `genre` ". "FROM Song ". "JOIN Track ON Song.id=Track.songId ". "JOIN Album ON Track.albumId=Album.id ". "JOIN Artist ON Album.artistId=Artist.id ". "JOIN Cover ON Album.coverId=Cover.id ". "JOIN Genre ON Album.genreId=Genre.id "; public static function init() { self::$db = new Database("localhost", "fauvet", "toto", "fauvet"); self::$db->connect(); } public static function findSongById($id) { $sql = self::$BASE_QUERY."WHERE Song.id = ?"; $stmt = self::$db->query($sql, [$id]); return $stmt->fetchAll(); } public static function findAllSongByTitleContaining($title) { $sql = self::$BASE_QUERY."WHERE Song.name LIKE ?;"; $stmt = self::$db->query($sql, ['%' . $title . '%']); return $stmt->fetchAll(); } public static function findSongByAlbumName($a) { $sql = self::$BASE_QUERY."WHERE Album.name LIKE ?;"; $stmt = self::$db->query($sql, ['%' . $a . '%']); return $stmt->fetchAll(); } public static function findAllSong() { $sql = self::$BASE_QUERY.";"; $stmt = self::$db->query($sql); return $stmt->fetchAll(); } //Fonctions pour la recherche d'album public static function findAlbumById($id) { $sql = "SELECT Album.id, Album.name, Album.year FROM Album WHERE Album.id=?;"; $stmt = self::$db->query($sql, [$id]); return $stmt->fetchAll(); } public static function findAlbumByNameContaining($n) { $sql = "SELECT Album.id, Album.name, Album.year FROM Album WHERE Album.name LIKE ?;"; $stmt = self::$db->query($sql, ['%'.$n.'%']); return $stmt->fetchAll(); } public static function findAllAlbum() { $sql = "SELECT Album.id, Album.name, Album.year FROM Album;"; $stmt = self::$db->query($sql); return $stmt->fetchAll(); } //Fonctions pour la recherche de titres public static function findArtsistById($id) { $sql = "SELECT Artist.id, Artist.name FROM Artist WHERE Artist.id=?;"; $stmt = self::$db->query($sql, [$id]); return $stmt->fetchAll(); } public static function findArtsistByNameContaining($n) { $sql = "SELECT Artist.id, Artist.name FROM Artist WHERE Artist.name LIKE ?;"; $stmt = self::$db->query($sql, ['%'.$n.'%']); return $stmt->fetchAll(); } public static function findAllArtist() { $sql = "SELECT * FROM Artist;"; $stmt = self::$db->query($sql); return $stmt->fetchAll(); } } ?>