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/test/test_db_connection.php
T
wsh5485 0eb0cf12fb chore: 删除测试和调试相关文件
移除不再需要的测试脚本、调试页面和解决方案文档,包括:
- 各种测试PHP文件(test.php, test_filters.php等)
- VIP功能测试和调试页面(test_vip*.php, vip_debug_page.html等)
- 数据库连接测试脚本(test_db_connection.php)
- 解决方案文档(SOLUTIONS.md, VIP_*_Report.md)
2025-12-05 01:38:06 +08:00

206 lines
9.4 KiB
PHP

<?php
header('Content-Type: text/html; charset=utf-8');
// 引入配置文件
require_once __DIR__ . '/config.php';
// 创建调试输出函数
function debug($message) {
echo "<div style='background: #f0f0f0; padding: 10px; margin: 5px; border-left: 4px solid #3498db;'>$message</div>";
}
debug("<strong>开始数据库连接测试</strong>");
// 检查配置变量
if (!isset($host) || !isset($username) || !isset($password) || !isset($database)) {
debug("<span style='color: red;'>错误:配置文件加载失败,缺少必要的配置变量</span>");
exit;
}
debug("配置信息检查通过:host=$host, database=$database, username=$username");
// 尝试连接数据库
try {
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
debug("<span style='color: green;'>成功连接到数据库!</span>");
// 检查vip_customers表是否存在
debug("<strong>检查vip_customers表是否存在</strong>");
$stmt = $pdo->query("SHOW TABLES LIKE 'vip_customers'");
$tableExists = $stmt->rowCount() > 0;
if ($tableExists) {
debug("<span style='color: green;'>vip_customers表存在</span>");
// 检查表结构
debug("<strong>vip_customers表结构</strong>");
$stmt = $pdo->query("DESCRIBE vip_customers");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
echo "<tr style='background: #3498db; color: white;'><th>字段名</th><th>类型</th><th>空值</th><th>键</th><th>默认值</th><th>额外信息</th></tr>";
foreach ($columns as $column) {
echo "<tr>";
echo "<td>{$column['Field']}</td>";
echo "<td>{$column['Type']}</td>";
echo "<td>{$column['Null']}</td>";
echo "<td>{$column['Key']}</td>";
echo "<td>{$column['Default']}</td>";
echo "<td>{$column['Extra']}</td>";
echo "</tr>";
}
echo "</table>";
// 检查数据量
debug("<strong>vip_customers表数据统计</strong>");
$stmt = $pdo->query("SELECT COUNT(*) as total FROM vip_customers");
$total = $stmt->fetchColumn();
debug("总记录数:$total");
// 检查激活的VIP客户数量
$stmt = $pdo->query("SELECT COUNT(*) as active FROM vip_customers WHERE is_active = 1");
$active = $stmt->fetchColumn();
debug("激活状态的VIP客户数:$active");
// 检查是否有测试手机号18699627661的记录
$stmt = $pdo->query("SELECT * FROM vip_customers WHERE phone LIKE '%18699627661%'");
$testPhoneExists = $stmt->rowCount() > 0;
debug("测试手机号18699627661存在:" . ($testPhoneExists ? "<span style='color: green;'>是</span>" : "<span style='color: red;'>否</span>"));
// 显示部分数据(最多5条)
debug("<strong>最近的VIP客户数据(最多5条)</strong>");
$stmt = $pdo->query("SELECT id, customer_name, phone, car_model, car_number, created_at FROM vip_customers ORDER BY created_at DESC LIMIT 5");
$recentVips = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($recentVips) > 0) {
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
echo "<tr style='background: #2ecc71; color: white;'><th>ID</th><th>客户姓名</th><th>手机号</th><th>车型</th><th>车牌号</th><th>创建时间</th></tr>";
foreach ($recentVips as $vip) {
echo "<tr>";
echo "<td>{$vip['id']}</td>";
echo "<td>{$vip['customer_name']}</td>";
echo "<td>{$vip['phone']}</td>";
echo "<td>{$vip['car_model']}</td>";
echo "<td>{$vip['car_number']}</td>";
echo "<td>{$vip['created_at']}</td>";
echo "</tr>";
}
echo "</table>";
} else {
debug("<span style='color: orange;'>vip_customers表中没有数据</span>");
}
// 如果表中没有数据,提供创建示例数据的选项
if ($total == 0) {
debug("<strong>需要创建示例数据</strong>");
echo "<form method='post' style='margin-top: 10px;'>";
echo "<input type='submit' name='create_sample_data' value='创建示例VIP客户数据' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
echo "</form>";
}
} else {
debug("<span style='color: red;'>错误:vip_customers表不存在</span>");
debug("<strong>需要创建vip_customers表</strong>");
echo "<form method='post' style='margin-top: 10px;'>";
echo "<input type='submit' name='create_table' value='创建vip_customers表' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
echo "</form>";
}
} catch(PDOException $e) {
debug("<span style='color: red;'>数据库连接失败:{$e->getMessage()}</span>");
// 检查是否是数据库不存在的错误
if (strpos($e->getMessage(), 'Unknown database') !== false) {
debug("<strong>数据库不存在,需要创建</strong>");
echo "<form method='post' style='margin-top: 10px;'>";
echo "<input type='submit' name='create_database' value='创建数据库' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
echo "</form>";
}
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 创建数据库
if (isset($_POST['create_database'])) {
try {
// 先连接MySQL服务器,不指定数据库
$pdo_root = new PDO("mysql:host=$host;charset=utf8mb4", $username, $password);
$pdo_root->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 创建数据库
$pdo_root->exec("CREATE DATABASE IF NOT EXISTS `$database` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
debug("<span style='color: green;'>数据库创建成功!</span>");
// 重定向刷新页面
header("Location: test_db_connection.php");
exit;
} catch(PDOException $e) {
debug("<span style='color: red;'>创建数据库失败:{$e->getMessage()}</span>");
}
}
// 创建表
if (isset($_POST['create_table'])) {
try {
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 创建vip_customers表
$createTableSQL = "
CREATE TABLE IF NOT EXISTS vip_customers (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL UNIQUE,
car_model VARCHAR(100),
car_number VARCHAR(50),
email VARCHAR(100),
birthday DATE,
is_active TINYINT DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$pdo->exec($createTableSQL);
debug("<span style='color: green;'>vip_customers表创建成功!</span>");
// 重定向刷新页面
header("Location: test_db_connection.php");
exit;
} catch(PDOException $e) {
debug("<span style='color: red;'>创建表失败:{$e->getMessage()}</span>");
}
}
// 创建示例数据
if (isset($_POST['create_sample_data'])) {
try {
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 插入示例VIP客户数据
$sampleData = [
['张三', '18612345678', '奔驰C级', '京A12345', 'zhangsan@example.com'],
['李四', '13987654321', '宝马3系', '京B54321', 'lisi@example.com'],
['王五', '18699627661', '奥迪A4', '沪A12345', 'wangwu@example.com'],
['赵六', '18699627777', '特斯拉Model 3', '深A67890', 'zhaoliu@example.com'],
['钱七', '13812345678', '丰田凯美瑞', '广A12345', 'qianqi@example.com']
];
$stmt = $pdo->prepare("INSERT INTO vip_customers (customer_name, phone, car_model, car_number, email) VALUES (?, ?, ?, ?, ?)");
foreach ($sampleData as $data) {
$stmt->execute($data);
}
debug("<span style='color: green;'>示例VIP客户数据创建成功!</span>");
// 重定向刷新页面
header("Location: test_db_connection.php");
exit;
} catch(PDOException $e) {
debug("<span style='color: red;'>创建示例数据失败:{$e->getMessage()}</span>");
}
}
}
?>