<?php
// db.php - centralized MySQL connection
// Place this file in your project root or a folder like 'includes/'
// Make sure all pages that need DB include this file: require_once 'db.php';

$host     = "localhost";         // usually 'localhost'
$db_name  = "kkfxoaad_bella";    // your database name
$db_user  = "kkfxoaad";          // database username
$db_pass  = "Bitok@2025";        // database password
$charset  = "utf8mb4";

$conn = null;

try {
    $dsn = "mysql:host=$host;dbname=$db_name;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, // throw exceptions
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,       // fetch assoc arrays
        PDO::ATTR_EMULATE_PREPARES   => false,                  // use native prepares
    ];
    
    // Create PDO instance
    $conn = new PDO($dsn, $db_user, $db_pass, $options);

} catch (PDOException $e) {
    // If connection fails, stop execution and show error
    http_response_code(500);
    echo "Database connection failed: " . htmlspecialchars($e->getMessage());
    exit();
}

// Optional: For backward compatibility, also create mysqli connection
$mysqli = new mysqli($host, $db_user, $db_pass, $db_name);
if ($mysqli->connect_error) {
    http_response_code(500);
    echo "MySQLi connection failed: " . htmlspecialchars($mysqli->connect_error);
    exit();
}
?>
