kiit-kaffe / api /db.php
Kush-Singh-26
Show DB error details in response
d3bf0f8
Raw
History Blame Contribute Delete
767 Bytes
<?php
$host = getenv('DB_HOST') ?: "localhost";
$user = getenv('DB_USER') ?: "root";
$pass = getenv('DB_PASS') ?: "";
$db = getenv('DB_NAME') ?: "kiit_kaffe_db";
try {
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
header("Content-Type: application/json");
$error = [
"status" => "error",
"message" => "Database connection failed",
"details" => $e->getMessage(),
"config" => [
"host" => $host,
"db" => $db,
"user" => $user
]
];
echo json_encode($error);
exit;
}
?>