<?php
// db.php — conexión central a MySQL
// Requiere config.php con las constantes definidas

$cfg = dirname(__FILE__) . '/config.php';
if (file_exists($cfg)) require_once $cfg;

$db_host = defined('DB_HOST') ? DB_HOST : 'localhost';
$db_name = defined('DB_NAME') ? DB_NAME : '';
$db_user = defined('DB_USER') ? DB_USER : '';
$db_pass = defined('DB_PASS') ? DB_PASS : '';

if (empty($db_name)) {
    // Sin config aún — devuelve null silenciosamente
    $pdo = null;
} else {
    try {
        $pdo = new PDO(
            "mysql:host={$db_host};dbname={$db_name};charset=utf8mb4",
            $db_user, $db_pass,
            [
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES   => false,
            ]
        );
    } catch (PDOException $e) {
        $pdo = null;
    }
}
