<?php
// includes/db.php
$config = require __DIR__ . '/config.php';
$port = $config['db']['port'] ?? null;
$dsn = "mysql:host={$config['db']['host']};" . ($port ? "port={$port};" : '') . "dbname={$config['db']['name']};charset={$config['db']['charset']}";
try {
  $pdo = new PDO($dsn, $config['db']['user'], $config['db']['pass'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  ]);
} catch (Exception $e) {
  http_response_code(500);
  echo 'Database connection error';
  exit;
}
