<?php

require_once "{$_SERVER['DOCUMENT_ROOT']}/config.php";

$GLOBALS['conn'] = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DB, DB_PORT);
if (!$GLOBALS['conn']) {
    echo "Error connecting to the database." . PHP_EOL;
    die();
}

try {
    $dsn = "mysql:host=" . DB_SERVER . ";dbname=" . DB_DB . ";port=" . DB_PORT . ";charset=utf8mb4";
    $username = DB_USER;
    $password = DB_PASSWORD;

    $GLOBALS['pdo'] = new PDO($dsn, $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Throw exceptions on errors
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,       // Fetch associative arrays by default
        PDO::ATTR_EMULATE_PREPARES => false,                  // Use real prepared statements
    ]);
} catch (PDOException $e) {
    echo "Error connecting to the database: " . $e->getMessage() . PHP_EOL;
    die();
}