-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
37 lines (32 loc) · 1.05 KB
/
Copy pathdb.php
File metadata and controls
37 lines (32 loc) · 1.05 KB
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
<?php
class Database {
private static $instance = null;
private $pdo;
private function __construct() {
$host = "localhost";
$dbname = "filmes";
$user = "root";
$pass = "";
try {
// Estabelece a conexão com o banco de dados
$this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
} catch (PDOException $e) {
// Envia erro como JSON e encerra o script
header('Content-Type: application/json');
echo json_encode(["error" => "Falha na conexão: " . $e->getMessage()]);
exit; // Garante que nada mais será executado
}
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Database();
}
return self::$instance;
}
public function getConnection() {
return $this->pdo;
}
}