mysql数据库数据字典生成代码
4258
这篇教程教大家做一个自动生成数据库数据字典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文件
本文网址:https://www.zztuku.com/detail-9788.html
站长图库 - mysql数据库数据字典生成代码
申明:如有侵犯,请 联系我们 删除。
您还没有登录,请 登录 后发表评论!
提示:请勿发布广告垃圾评论,否则封号处理!!