rpg-westreiche/westreiche_website/app/db.php

34 lines
943 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/** Westreiche – Datenbankverbindung (PDO, MariaDB/MySQL). */
function db(): PDO
{
static $pdo = null;
if ($pdo instanceof PDO) {
return $pdo;
}
$db = (array) config('db', []);
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
(string) ($db['host'] ?? 'localhost'),
(int) ($db['port'] ?? 3306),
(string) ($db['name'] ?? ''),
(string) ($db['charset'] ?? 'utf8mb4')
);
$pdo = new PDO($dsn, (string) ($db['user'] ?? ''), (string) ($db['password'] ?? ''), [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_STRINGIFY_FETCHES => false,
]);
$pdo->exec("SET time_zone = '+00:00'");
if (function_exists('ws_migrations_apply')) {
ws_migrations_apply($pdo);
}
return $pdo;
}