From f4b12aa7431d6a5cd718532ec54788a61cd032c7 Mon Sep 17 00:00:00 2001 From: lukibeg Date: Fri, 24 Oct 2025 23:21:43 -0300 Subject: [PATCH] NEW: Creating Database.php class to assign all responsability of DB connection in this archive. --- app/models/Database.php | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/models/Database.php diff --git a/app/models/Database.php b/app/models/Database.php new file mode 100644 index 0000000..f02c27a --- /dev/null +++ b/app/models/Database.php @@ -0,0 +1,42 @@ +hostname = $_ENV['DB_HOST']; + $this->username = $_ENV['DB_USER']; + $this->password = $_ENV['DB_PASSWORD']; + $this->database = $_ENV['DB']; + } + + public function dbConnect() + { + + + if (self::$connection === null) { + try { + self::$connection = new \PDO( + 'mysql:host=' . $this->hostname . ';' . + 'dbname=' . $this->database . ';' . + 'charset=utf8mb4', + $this->username, + $this->password + ); + self::$connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + } catch (\PDOException $e) { + return json_encode(['error' => 'Erro na conexão com o banco ' . $e->getMessage()]); + } + } + return self::$connection; + } +}