38 lines
866 B
PHP
38 lines
866 B
PHP
<?php
|
|
|
|
declare(strict_types=1); // Erzwingt strikte Typenprüfung für Parameter und Rückgabewerte -> keine automatische Typenumwandlung
|
|
|
|
/**
|
|
* Liefert eine MySQLi-Verbindung anhand der zentralen Konfiguration.
|
|
*
|
|
* Nutzung:
|
|
* $conn = db_connect();
|
|
*/
|
|
function db_connect(): mysqli
|
|
{
|
|
static $cfg;
|
|
|
|
if ($cfg === null)
|
|
{
|
|
/** @var array{db: array{host:string,port:int,user:string,pass:string,name:string,charset:string}} $cfg */
|
|
$cfg = require __DIR__ . '/config.php';
|
|
}
|
|
|
|
$db = $cfg['db'];
|
|
|
|
$conn = new mysqli($db['host'], $db['user'], $db['pass'], $db['name'], $db['port']);
|
|
|
|
if ($conn->connect_error)
|
|
{
|
|
http_response_code(500);
|
|
die('Datenbankfehler');
|
|
}
|
|
|
|
// Einheitliches Charset (wichtig für Umlaute/Emojis & Sicherheit)
|
|
$conn->set_charset($db['charset']);
|
|
|
|
return $conn;
|
|
}
|
|
|
|
?>
|