You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP引擎数据展示表格优化请求:修正代码与布局

修正后的PHP连接MySQL生成规范HTML表格代码

我帮你调整了代码,解决了可能存在的数据库连接、查询逻辑问题,同时规范了HTML表格的布局,让数据展示更清晰专业。以下是完整代码:

<?php
// 数据库配置信息
$host = 'localhost';
$dbname = 'your_database'; // 替换为你的数据库名
$username = 'your_username'; // 替换为你的数据库用户名
$password = 'your_password'; // 替换为你的数据库密码

// 创建MySQLi连接
$conn = new mysqli($host, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 准备查询语句(生产环境建议使用预处理语句防止SQL注入)
$sql = "SELECT location, brand, model, origin, mfgdate FROM engines";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Engines 数据列表</title>
    <style>
        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
            font-family: Arial, sans-serif;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .no-data {
            text-align: center;
            color: #666;
            padding: 20px;
        }
    </style>
</head>
<body>
    <h2 style="text-align: center;">Engines 设备信息表</h2>
    <table>
        <thead>
            <tr>
                <th>位置</th>
                <th>品牌</th>
                <th>型号</th>
                <th>产地</th>
                <th>制造日期</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                // 输出每行数据
                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . htmlspecialchars($row["location"]) . "</td>";
                    echo "<td>" . htmlspecialchars($row["brand"]) . "</td>";
                    echo "<td>" . htmlspecialchars($row["model"]) . "</td>";
                    echo "<td>" . htmlspecialchars($row["origin"]) . "</td>";
                    echo "<td>" . htmlspecialchars($row["mfgdate"]) . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='5' class='no-data'>暂无数据</td></tr>";
            }
            // 释放结果集并关闭连接
            $result->free();
            $conn->close();
            ?>
        </tbody>
    </table>
</body>
</html>

关键修正与优化说明

  • 数据库连接优化:替换了过时的mysql_*系列函数,改用更安全、支持面向对象的mysqli扩展,同时添加了连接错误处理,方便排查问题。
  • 安全防护:使用htmlspecialchars()转义输出数据,避免XSS攻击;生产环境建议进一步使用预处理语句(mysqli_prepare)来防范SQL注入。
  • 表格布局规范:
    • 使用<thead>和<tbody>语义化标签,提升HTML结构的可读性和可维护性。
    • 添加了基础CSS样式,包括边框合并、表头高亮、奇偶行区分、悬停效果,让表格更易读。
    • 设置表格居中、宽度自适应,适配不同屏幕。
  • 异常处理:增加了无数据时的友好提示,避免表格空白。
  • 资源释放:主动释放结果集并关闭数据库连接,避免资源浪费。

当你的engines表中存在location=test、brand=test1、model=test2、origin=test3、mfgdate=日期的记录时,表格会正确渲染出对应数据。

内容的提问来源于stack exchange,提问作者Mohamed Meeshaag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:26:09