This repository has been archived on 2026-06-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
carwash_order/get_vip_customer.php
wsh5485 76ba21f57a refactor(数据库配置): 重构数据库配置引入方式
使用根目录的配置文件统一管理数据库连接配置
添加配置加载失败的错误处理
保持变量名兼容性
2025-11-19 12:25:08 +08:00

58 lines
1.7 KiB
PHP

<?php
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
// 获取客户ID
$id = $_GET['id'] ?? null;
if (!$id) {
http_response_code(400);
echo json_encode(['error' => '缺少客户ID参数'], JSON_UNESCAPED_UNICODE);
exit;
}
// 引入根目录的数据库配置文件
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);
// 查询指定ID的VIP客户
$stmt = $pdo->prepare("
SELECT id, customer_name, phone, car_model, car_number, email, birthday, notes, is_active
FROM vip_customers
WHERE id = ? AND is_active = 1
");
$stmt->execute([$id]);
$vipCustomer = $stmt->fetch(PDO::FETCH_ASSOC);
if ($vipCustomer) {
echo json_encode($vipCustomer, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} else {
http_response_code(404);
echo json_encode(['error' => 'VIP客户不存在'], JSON_UNESCAPED_UNICODE);
}
} catch(PDOException $e) {
http_response_code(500);
echo json_encode([
'error' => '数据库连接失败',
'message' => $e->getMessage()
], JSON_UNESCAPED_UNICODE);
}
?>