From 57377ea4b8730b34a3618ab2163e1de83d67237f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=B1=95=E9=B9=8F?= Date: Wed, 19 Nov 2025 12:23:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D?= =?UTF-8?q?=E7=BD=AE):=20=E5=B0=86=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=A7=BB=E8=87=B3=E5=A4=96=E9=83=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构数据库连接配置,从独立文件引入配置并添加配置检查 确保配置变量存在时才会继续执行,提高代码健壮性 --- get_vip_customers.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/get_vip_customers.php b/get_vip_customers.php index 605ef42..d696d45 100644 --- a/get_vip_customers.php +++ b/get_vip_customers.php @@ -2,11 +2,21 @@ header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: no-cache, must-revalidate'); -// 数据库连接配置 -$host = 'localhost'; -$dbname = 'carwash_booking'; -$username = 'root'; -$password = ''; +// 引入根目录的数据库配置文件 +require_once __DIR__ . '/config.php'; + +// 确保配置变量存在 +if (!isset($host) || !isset($username) || !isset($password) || !isset($database)) { + http_response_code(500); + echo json_encode([ + 'error' => '配置文件加载失败', + 'message' => '无法读取数据库配置信息' + ], JSON_UNESCAPED_UNICODE); + exit; +} + +// 配置变量重命名,确保兼容性 +$dbname = $database; try { $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);