這篇教程教大家做一個自動生成數據庫數據字典HTML文件的代碼,有時候在開發系統制作系統說明的時候很有用。不多說了,直接上代碼:
mysql字典生成代碼
<?php // -h host -u user -p password -P port -c charset -d dbname $param = getopt('h:u:p:P:c:d:'); $host = $param['h'] ?? '127.0.0.1'; $port = $param['P'] ?? 3306; $user = $param['u'] ?? 'root'; $pass = $param['p'] ?? '123456'; $charset = $param['c'] ?? 'utf8'; $dbname = $param['d'] ?? ''; $dsn = "mysql:dbname={$dbname};host={$host};port={$port}"; try { $pdo = new PDO($dsn, $user, $pass, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"]); } catch (PDOException $e) { echo 'Connection failed:' . $e->getMessage(); } //獲取數據庫中所有表信息 $sql = "SHOW TABLE STATUS FROM {$dbname}"; $result = $pdo->query($sql, PDO::FETCH_ASSOC); $tables = $result->fetchAll(); $table_count = count($tables); $html = '<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>' . $dbname . '--數據字典</title> <style type="text/css"> table caption, table th, table td { padding: 0.1em 0.5em 0.1em 0.5em; margin: 0.1em; vertical-align: top; } th { font-weight: bold; color: black; background: #D3DCE3; } table tr.odd th, .odd { background: #E5E5E5; } table tr.even th, .even { background: #f3f3f3; } .db_table{ border-top:1px solid #333; } .title{font-weight:bold;} </style> </head> <body> <div style="text-align:center;background:#D3DCE3;font-size:19px;"> <b>' . $dbname . '--數據字典</b> </div> <div style="background:#f3f3f3;text-align:center;">(注:共' . $table_count . '張表,按ctrl+F查找關鍵字)</div>' . "\n"; for ($i = 0; $i < $table_count; $i++) { $html .= '<ul type="square">' . "\n"; $html .= ' <li>'; $html .= ($i + 1) . '、表名:[' . $tables[$i]['Name'] . '] 注釋:' . $tables[$i]['Comment']; $html .= '</li>' . "\n"; //查詢數據庫表字段信息 $tab_name = $tables[$i]['Name']; $sql_tab = 'SHOW FULL FIELDS FROM `' . $tables[$i]['Name'] . '`'; $result = $pdo->query($sql_tab, PDO::FETCH_ASSOC); $field_info = $result->fetchAll(); $html .= '<li style="list-style: none outside none;"><table border="0" >'; $html .= '<tr> <th style="width:110px">字段</th> <th>類型</th> <th>為空</th> <th>額外</th> <th>默認</th> <th style="width:95px">字符集</th> <th>是否主鍵</th> <th>備注</th></tr>'; for ($j = 0; $j < count($field_info); $j++) { $html .= ' <tr class="' . ($j % 2 == 0 ? "odd" : "even") . '">' . "\n"; $html .= ' <td>' . $field_info[$j]['Field'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Type'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Null'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Extra'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Default'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Collation'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Key'] . '</td>' . "\n"; $html .= ' <td>' . $field_info[$j]['Comment']. '</td>' . "\n"; $html .= ' </tr>' . "\n"; } $html .= ' </table></li>' . "\n"; $html .= '</ul>' . "\n"; } $html .= '</body>' . "\n"; $html .= '</html>' . "\n"; file_put_contents($dbname .'.html', $html);
使用方法
php dict.php -h 127.0.0.1 -u root -p 123456 -P 3306 -d blog_service
參數說明
-h mysql服務器地址 -u mysql用戶名 -p mysql密碼 -P mysql端口 -d mysql數據庫名
參數都有默認值,-d參數必傳 最簡單的用法:
php dict.php -d blog_service
執行命令后生成一個以數據庫名命名的html文件