-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
43 lines (41 loc) · 1.47 KB
/
Copy pathdb.php
File metadata and controls
43 lines (41 loc) · 1.47 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
37
38
39
40
41
42
43
<?php
/**
* CINEMAX — DATABASE CONNECTION
* ============================================================
* Loads credentials from config.php (one level above web root)
* or falls back to environment variables.
*
* Place config.php here (NOT inside public_html):
* /home/youruser/config.php ← safe, not web-accessible
* /home/youruser/public_html/ ← web root (files go here)
* ============================================================
*/
$configPath = dirname(__DIR__) . '/config.php';
if (file_exists($configPath)) {
require_once $configPath;
}
$host = defined('DB_HOST') ? DB_HOST : ($_ENV['DB_HOST'] ?? '127.0.0.1');
$db = defined('DB_NAME') ? DB_NAME : ($_ENV['DB_NAME'] ?? 'cinema');
$user = defined('DB_USER') ? DB_USER : ($_ENV['DB_USER'] ?? 'root');
$pass = defined('DB_PASS') ? DB_PASS : ($_ENV['DB_PASS'] ?? '');
try {
$pdo = new PDO(
"mysql:host=$host;dbname=$db;charset=utf8mb4",
$user,
$pass,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
);
} catch (PDOException $e) {
http_response_code(500);
if (PHP_SAPI === 'cli') {
fwrite(STDERR, "Database connection failed.\n");
} else {
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Database connection failed.']);
}
exit;
}