57377ea4b8
重构数据库连接配置,从独立文件引入配置并添加配置检查 确保配置变量存在时才会继续执行,提高代码健壮性
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Cache-Control: no-cache, must-revalidate');
|
|
|
|
// 引入根目录的数据库配置文件
|
|
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);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// 查询所有VIP客户,按创建时间倒序排列
|
|
$stmt = $pdo->query("
|
|
SELECT id, customer_name, phone, car_model, car_number, email, birthday, is_active
|
|
FROM vip_customers
|
|
WHERE is_active = 1
|
|
ORDER BY created_at DESC
|
|
");
|
|
|
|
$vipCustomers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo json_encode($vipCustomers, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
|
|
} catch(PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'error' => '数据库连接失败',
|
|
'message' => $e->getMessage()
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
?>
|