35 lines
1.6 KiB
PHP
35 lines
1.6 KiB
PHP
<?php
|
|
// ============================================
|
|
// koneksi.php — Konfigurasi Database
|
|
// ============================================
|
|
|
|
$databaseUrl = getenv('DATABASE_URL') ?: getenv('MYSQL_URL') ?: '';
|
|
$databaseConfig = $databaseUrl ? parse_url($databaseUrl) : [];
|
|
$databasePath = isset($databaseConfig['path']) ? ltrim($databaseConfig['path'], '/') : '';
|
|
|
|
define('DB_HOST', getenv('DB_HOST') ?: getenv('MYSQL_HOST') ?: ($databaseConfig['host'] ?? 'localhost'));
|
|
define('DB_USER', getenv('DB_USER') ?: getenv('MYSQL_USER') ?: ($databaseConfig['user'] ?? 'root'));
|
|
define('DB_PASS', getenv('DB_PASS') ?: getenv('MYSQL_PASSWORD') ?: getenv('MYSQL_ROOT_PASSWORD') ?: ($databaseConfig['pass'] ?? ''));
|
|
define('DB_NAME', getenv('DB_NAME') ?: getenv('MYSQL_DATABASE') ?: ($databasePath ?: 'webgis_spbu'));
|
|
define('DB_PORT', (int) (getenv('DB_PORT') ?: getenv('MYSQL_PORT') ?: ($databaseConfig['port'] ?? 3306)));
|
|
|
|
// Mengaktifkan exception untuk mysqli agar error bisa ditangkap dengan try-catch
|
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
|
|
|
function getConnection(): mysqli {
|
|
try {
|
|
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
|
|
$conn->set_charset('utf8mb4');
|
|
return $conn;
|
|
} catch (mysqli_sql_exception $e) {
|
|
http_response_code(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Koneksi database gagal',
|
|
'error_detail' => $e->getMessage() // Detail error ini akan muncul di tab Network
|
|
]);
|
|
exit;
|
|
}
|
|
}
|