Update: 2026-05-03 17:32:57
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Simple PDO Database Wrapper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
@@ -6,7 +9,6 @@ namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Exception;
|
||||
|
||||
final class Database
|
||||
{
|
||||
@@ -15,24 +17,27 @@ final class Database
|
||||
public static function getInstance(): PDO
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$host = $_ENV['DB_HOST'];
|
||||
$db = $_ENV['DB_DATABASE'];
|
||||
$user = $_ENV['DB_USERNAME'];
|
||||
$pass = $_ENV['DB_PASSWORD'];
|
||||
$port = $_ENV['DB_PORT'];
|
||||
$charset = $_ENV['DB_CHARSET'] ?? 'utf8mb4';
|
||||
|
||||
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
$config = require APP_PATH . '/config/database.php';
|
||||
|
||||
$dsn = sprintf(
|
||||
"mysql:host=%s;port=%s;dbname=%s;charset=%s",
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['database'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
try {
|
||||
self::$instance = new PDO($dsn, $user, $pass, $options);
|
||||
self::$instance = new PDO($dsn, $config['username'], $config['password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("Database Connection Error: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user