<?php
// db/config.php
// RS Learning Lab — database configuration for XAMPP (MySQL root, blank password)

// Database connection settings
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'rs_lab';
$DB_PORT = 3306; // usually 3306

// Create MySQLi connection and check
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_PORT);

// Check connection
if ($mysqli->connect_errno) {
    // If you enabled display_errors (see below) you'll see this on the page.
    error_log("MySQL connection failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
    die("Database connection failed. Please check config.php settings.");
}

// Set charset to UTF-8
$mysqli->set_charset("utf8mb4");

// Optional: a PDO connection if any part of app expects PDO (safe to have both)
try {
    $dsn = "mysql:host={$DB_HOST};dbname={$DB_NAME};port={$DB_PORT};charset=utf8mb4";
    $pdo = new PDO($dsn, $DB_USER, $DB_PASS, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);
} catch (PDOException $e) {
    error_log("PDO connection failed: " . $e->getMessage());
    // don't reveal raw error to users in production
    // but during debugging it's okay to show:
    // die("PDO DB connection failed: " . $e->getMessage());
}

// If your app expects a single $db or $conn variable, create alias:
$db = $mysqli;   // mysqli object
$conn = $mysqli; // some files may use $conn

// End of config.php
