From 7b6cf424673e92807c05fee5f5bddf065086a4c3 Mon Sep 17 00:00:00 2001 From: Aleksandr Shelkovin Date: Fri, 7 Feb 2025 22:22:30 +0300 Subject: [PATCH] add db postgre --- engine/app/model/MusicModel.php | 6 ++ engine/core/DataBase.php | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 engine/core/DataBase.php diff --git a/engine/app/model/MusicModel.php b/engine/app/model/MusicModel.php index 4b6aa5e..66a8814 100644 --- a/engine/app/model/MusicModel.php +++ b/engine/app/model/MusicModel.php @@ -1,10 +1,16 @@ getDataBase('SELECT * FROM top_chart'); + var_dump($result); $box = []; if ($handle = opendir('./uploads')) { while (false !== ($entry = readdir($handle))) { diff --git a/engine/core/DataBase.php b/engine/core/DataBase.php new file mode 100644 index 0000000..2d89cf2 --- /dev/null +++ b/engine/core/DataBase.php @@ -0,0 +1,101 @@ +exec(" + CREATE TABLE IF NOT EXISTS top_chart ( + id INT(4) serial PRIMARY KEY, + name varchar(100), + url varchar(100), + )"); + } + + + public function openDataBase() + { + if (!isset(self::$db)) { + // Execute code catching potential exceptions + try { + // $options = array(\PDO::ATTR_PERSISTENT => DB_PERSISTENCY, \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'); + self::$db = new \PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD); + // self::$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + // $this->createTable(self::$db); + } catch (\PDOException $e) { + // Close the database handler and trigger an error + self::Close(); + trigger_error($e->getMessage(), E_USER_ERROR); + } + } + return self::$db; + } + + public function lastInsertId() + { + $db = $this->openDataBase(); + return $db->lastInsertId(); + } + + public function setDataBase($string) + { + $db = $this->openDataBase(); + $db->exec($string); + } + + public function setBasePrepare($query, $data) + { + $db = $this->openDataBase(); + $stmt = $db->prepare($query); + $stmt->execute($data); + } + + public function getDataBase($string) + { + $db = $this->openDataBase(); + $query = $db->query($string); + if ($query) { + return $query->fetchAll(\PDO::FETCH_ASSOC); + } else { + return false; + } + } + + public function rowCount($query, $data) + { + $db = $this->openDataBase(); + $stmt = $db->prepare($query); + $stmt->execute($data); + return $stmt->rowCount(); + } + + public function getBasePrepare($query, $data) + { + $db = $this->openDataBase(); + $stmt = $db->prepare($query); + $stmt->execute($data); + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } +}