-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
39 lines (34 loc) · 1.07 KB
/
db.php
File metadata and controls
39 lines (34 loc) · 1.07 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
<?php
ini_set('display_errors', '0');
error_reporting(E_ALL);
function env($key, $default = null) {
static $vars;
if ($vars === null) {
$vars = [];
$envPath = __DIR__ . '/.env';
if (is_readable($envPath)) {
foreach (file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (str_starts_with(trim($line), '#')) continue;
[$k, $v] = array_pad(explode('=', $line, 2), 2, null);
$vars[trim($k)] = $v !== null ? trim($v) : '';
}
}
}
return $vars[$key] ?? $default;
}
function db() {
static $pdo;
if ($pdo) return $pdo;
$host = env('DB_HOST', '127.0.0.1');
$name = env('DB_NAME', 'dashboard_demo');
$user = env('DB_USER', 'demo_user');
$pass = env('DB_PASS', 'demo_pass');
$dsn = "mysql:host=$host;dbname=$name;charset=utf8mb4";
$opts = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opts);
return $pdo;
}