Files
radio-free-elsewhere/includes/db.php
T
Daniel MesserandClaude Opus 5.5 9a1e8a33a6 Add database connection helper and live now-playing page
- includes/db.php: getDB() returns a shared PDO connection to MariaDB
- config/db_credentials.example.php: credentials template; the real
  db_credentials.php is gitignored and deployed separately via SFTP
- live.php: page for active broadcasts, with the player on the left
  and a placeholder Recently Played table on the right

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 06:20:07 -05:00

31 lines
935 B
PHP

<?php
// Shared database connection. Include this file and call getDB() to get a PDO handle.
if (basename($_SERVER['SCRIPT_FILENAME'] ?? '') === basename(__FILE__)) {
http_response_code(403);
exit;
}
function getDB(): PDO {
static $pdo = null;
if ($pdo !== null) return $pdo;
$credentialsFile = __DIR__ . '/../config/db_credentials.php';
if (!is_readable($credentialsFile)) {
throw new RuntimeException('Database credentials file is missing.');
}
$cfg = require $credentialsFile;
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
$cfg['host'], $cfg['port'], $cfg['database'], $cfg['charset']
);
$pdo = new PDO($dsn, $cfg['username'], $cfg['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
return $pdo;
}