initiall comit

This commit is contained in:
unknown
2026-06-12 22:44:23 +07:00
parent 4949c84feb
commit d7dd18ddba
9 changed files with 1069 additions and 15 deletions
+27 -11
View File
@@ -18,19 +18,35 @@ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
// Baca dari Environment Variable (untuk deployment Coolify/server)
// Jika tidak ada, fallback ke konfigurasi XAMPP lokal
$host = getenv('DB_HOST') ?: "localhost";
$username = getenv('DB_USER') ?: "root";
$password = getenv('DB_PASSWORD') ?: "";
$database = getenv('DB_DATABASE') ?: "pemetaan_kemiskinan";
$port = getenv('DB_PORT') ?: 3306;
$host = getenv('DB_HOST') ?: ($_SERVER['DB_HOST'] ?? ($_ENV['DB_HOST'] ?? "localhost"));
$username = getenv('DB_USER') ?: ($_SERVER['DB_USER'] ?? ($_ENV['DB_USER'] ?? "root"));
$password = getenv('DB_PASSWORD') ?: ($_SERVER['DB_PASSWORD'] ?? ($_ENV['DB_PASSWORD'] ?? ""));
$database = getenv('DB_DATABASE') ?: ($_SERVER['DB_DATABASE'] ?? ($_ENV['DB_DATABASE'] ?? "pemetaan_kemiskinan"));
$port = getenv('DB_PORT') ?: ($_SERVER['DB_PORT'] ?? ($_ENV['DB_PORT'] ?? 3306));
// KONEKSI MYSQLI
$conn = new mysqli($host, $username, $password, $database, (int)$port);
// KONEKSI MYSQLI DENGAN LOGIKA FALLBACK
$conn = null;
// CEK KONEKSI
if ($conn->connect_error) {
sendJSON('error', 'Koneksi database gagal: ' . $conn->connect_error);
exit();
// Aktifkan throwing exception untuk mysqli agar try-catch berfungsi dengan baik
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
// 1. Coba koneksi utama sesuai konfigurasi Environment / Env File
$conn = new mysqli($host, $username, $password, $database, (int)$port);
} catch (Exception $e) {
// 2. Coba koneksi Fallback ke Docker Host (host.docker.internal)
try {
$conn = new mysqli("host.docker.internal", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_docker) {
// 3. Coba koneksi Fallback ke Localhost murni
try {
$conn = new mysqli("localhost", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_local) {
// Jika semua gagal, berikan respon error
sendJSON('error', 'Koneksi database utama & fallback gagal: ' . $e_local->getMessage());
exit();
}
}
}
// SET CHARSET UTF-8