專案開發中,資料字典是團隊溝通、新人上手、維護重構的關鍵文件。手工維護極易脫節,本文提供一支 PHP 腳本,自動讀取 MySQL 中繼資料,生成美觀的 HTML 資料字典,支援表註解、欄位型別、主鍵、預設值、編碼等完整資訊。
<?php
header('Content-Type: text/html; charset=utf-8');
// ===== 修改這 6 個常數即可部署 =====
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'your_password');
define('DB_NAME', 'your_database');
define('DB_PORT', 3306);
define('DB_CHAR', 'utf8mb4');
define('APP_NAME', '我的專案');
// ===================================
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
if (!$conn) die('資料庫連線失敗: ' . mysqli_connect_error());
mysqli_set_charset($conn, DB_CHAR);
// 取得所有資料表狀態(含註解)
$sql = "SHOW TABLE STATUS FROM `" . DB_NAME . "`";
$result = mysqli_query($conn, $sql);
$tables = [];
while ($row = mysqli_fetch_assoc($result)) {
$tables[] = $row;
}
$tableCount = count($tables);
// 輸出 HTML 頭部
echo '<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>' . APP_NAME . ' - 資料字典</title>
<style>
body { font-family: "Microsoft JhengHei", sans-serif; margin: 20px; background: #fafafa; }
.header { background: #2c3e50; color: white; padding: 20px; text-align: center; border-radius: 8px 8px 0 0; }
.info-bar { background: #ecf0f1; padding: 10px; text-align: center; color: #7f8c8d; border-radius: 0 0 8px 8px; }
.table-card { background: white; margin: 20px 0; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden; }
.table-title { background: #34495e; color: white; padding: 15px 20px; font-size: 1.1em; font-weight: bold; }
.table-title span { color: #3498db; font-weight: normal; margin-left: 10px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid #eee; }
th { background: #f8f9fa; font-weight: 600; color: #2c3e50; position: sticky; top: 0; }
tr:hover { background: #f8f9fa; }
tr:nth-child(even) { background: #fdfdfd; }
.pk { color: #e74c3c; font-weight: bold; }
.null-yes { color: #27ae60; }
.null-no { color: #e74c3c; }
@media (max-width: 768px) { .table-card { overflow-x: auto; } }
</style>
</head>
<body>
<div class="header"><h1>' . APP_NAME . ' 資料字典</h1></div>
<div class="info-bar">共 ' . $tableCount . ' 張表 | 產生時間: ' . date('Y-m-d H:i:s') . ' | 提示: Ctrl+F 搜尋關鍵字</div>';
foreach ($tables as $i => $table) {
$tableName = $table['Name'];
$tableComment = $table['Comment'] ?: '(無註解)';
// 取得欄位詳細資訊
$fieldSql = "SHOW FULL COLUMNS FROM `" . $tableName . "`";
$fieldResult = mysqli_query($conn, $fieldSql);
$fields = [];
while ($f = mysqli_fetch_assoc($fieldResult)) $fields[] = $f;
// 取得主鍵資訊
$keySql = "SHOW KEYS FROM `" . $tableName . "` WHERE Key_name = 'PRIMARY'";
$keyResult = mysqli_query($conn, $keySql);
$pkFields = [];
while ($k = mysqli_fetch_assoc($keyResult)) $pkFields[] = $k['Column_name'];
echo '<div class="table-card">';
echo '<div class="table-title">' . ($i+1) . '. ' . $tableName . ' <span>' . htmlspecialchars($tableComment) . '</span></div>';
echo '<table><thead><tr>';
echo '<th style="width:140px">欄位名稱</th>';
echo '<th style="width:160px">資料型別</th>';
echo '<th style="width:80px">允許空值</th>';
echo '<th style="width:100px">預設值</th>';
echo '<th style="width:100px">編碼/排序</th>';
echo '<th>備註</th>';
echo '</tr></thead><tbody>';
foreach ($fields as $field) {
$isPk = in_array($field['Field'], $pkFields) ? 'PK' : '';
$pkClass = $isPk ? 'pk' : '';
$nullClass = $field['Null'] === 'YES' ? 'null-yes' : 'null-no';
$nullText = $field['Null'] === 'YES' ? 'YES' : 'NO';
$default = $field['Default'] !== null ? htmlspecialchars($field['Default']) : '(NULL)';
echo '<tr>';
echo '<td class="' . $pkClass . '">' . $field['Field'] . ($isPk ? ' <span class="pk">[PK]</span>' : '') . '</td>';
echo '<td>' . $field['Type'] . '</td>';
echo '<td class="' . $nullClass . '">' . $nullText . '</td>';
echo '<td>' . $default . '</td>';
echo '<td>' . ($field['Collation'] ?: '-') . '</td>';
echo '<td>' . htmlspecialchars($field['Comment'] ?: '-') . '</td>';
echo '</tr>';
}
echo '</tbody></table></div>';
}
echo '</body></html>';
mysqli_close($conn);
| 功能 | 實作建議 |
|---|---|
| 匯出 Excel/Markdown | 引入 PhpSpreadsheet 或輸出 Markdown 表格語法 |
| 支援多資料庫切換 | 加入下拉選單切換 DB_NAME,透過 GET 參數傳遞 |
| 欄位關聯關係圖 | 解析 FOREIGN KEY,繪製 Mermaid.js 關係圖 |
| 版本比對 | 儲存歷史快照,比對結構變更(新增/刪除/修改欄位) |
| 權限控管 | 加入 Basic Auth 或整合專案登入系統 |
這支資料字典生成器零依賴、單檔部署、介面美觀,能即時反映資料庫最新結構。建議納入專案部署流程,每次資料庫遷移後自動重新生成,讓文件永遠與代碼同步。