之前在新接触已经成形的项目时, 需要经常检索数据库中的表及字段, 来快速理解项目和编码, 就写了一个好看又实用的数据库结构查看html
只需要修改代码9-12行的数据库配置, 即可运行展示出来
源码献上
<?php
/**
* 生成mysql数据字典
* @author yynan<904381790@qq.com>
*/
header ( "Content-type: text/html; charset=utf-8" );
// 配置数据库
$dbserver = "127.0.0.1";
$dbusername = "root";
$dbpassword = "root";
$database = !empty($_GET['db']) ? $_GET['db'] : "test";
// 其他配置
$title = '数据字典['.$database.']';
$mysql_conn = @mysqli_connect ( "$dbserver", "$dbusername", "$dbpassword", $database ) or die ( "Mysql connect is error." );
mysqli_query ( $mysql_conn, 'SET NAMES utf8');
$table_result = mysqli_query ( $mysql_conn, 'show tables' );
// 取得所有的表名
while ( $row = mysqli_fetch_array ( $table_result ) ) {
$tables [] ['TABLE_NAME'] = $row [0];
}
// 循环取得所有表的备注及表中列消息
foreach ( $tables as $k => $v ) {
$sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.TABLES ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
$table_result = mysqli_query ( $mysql_conn, $sql );
while ( $t = mysqli_fetch_assoc( $table_result ) ) {
// var_dump($t);exit;
$tables [$k] ['TABLE_CN_NAME'] = $t['TABLE_COMMENT'];
$tables [$k] ['TABLE_COMMENT'] = $t['TABLE_COMMENT'] . ' [rows:'.number_format($t ['TABLE_ROWS']).']';
}
$sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.COLUMNS ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
$fields = array ();
$field_result = mysqli_query ( $mysql_conn, $sql );
while ( $t = mysqli_fetch_assoc( $field_result ) ) {
$fields [] = $t;
}
$tables [$k] ['COLUMN'] = $fields;
}
$html = '';// $html = '<div style="float:right">'; // 适应手机的代码
$mulu = '<div style="position:fixed;top:0px;overflow:scroll;max-width:400px;bottom:0;">
<ul style="size:14px;background-color: rgb(220, 220, 220);opacity:0.85;font-weight:bold;padding:5px;">'; // 目录
// 循环所有表
foreach ( $tables as $k => $v ) {
$muluCn = !empty($v['TABLE_CN_NAME'])?' <span style="color:#1056d6;font-weight: normal;">'.$v['TABLE_CN_NAME'].'</span>':'';
$mulu .= '<li><a style="text-decoration:none;color:#000" href="#'.$v ['TABLE_NAME'].'">' . $v ['TABLE_NAME'] .$muluCn. '</a></li>';
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption><a name="'.$v ['TABLE_NAME'].'">' . $v ['TABLE_NAME'] . ' ' . $v ['TABLE_COMMENT'] . '</a></caption>';
$html .= '<tbody><tr><th>字段名</th><th>备注</th><th>数据类型[默认值]</th></tr>';
$html .= '';
foreach ( $v ['COLUMN'] as $f ) {
$html .= '<tr><td class="c1">' . $f ['COLUMN_NAME'] . '</td>';
$html .= '<td class="c6"> ' . $f ['COLUMN_COMMENT'] . '</td>';
$html .= '<td class="c2">' . $f ['COLUMN_TYPE'] .' <div style="float:right">['.$f ['COLUMN_DEFAULT'].']</div>'. '</td>';
$html .= '</tr>';
}
$r = mysqli_query ( $mysql_conn, 'show index from '.$v['TABLE_NAME'] );
$_tmp_index_arr = [];
while ( $t = mysqli_fetch_assoc ( $r ) ) {
if(isset($_tmp_index_arr[$t ['Key_name']])){
$_tmp_index_arr[$t ['Key_name']]['Column_name'] .= ', '.$t ['Column_name'];
}else{
$_tmp_index_arr[$t ['Key_name']]['Key_name'] = $t ['Key_name'];
$_tmp_index_arr[$t ['Key_name']]['Column_name'] = $t ['Column_name'];
$_tmp_index_arr[$t['Key_name']]['Index_type'] = $t['Index_type'];
$_tmp_index_arr[$t['Key_name']]['title'] = ($t['Non_unique'] === '0') ? 'UNIQUE' : 'INDEX';
}
}
foreach ($_tmp_index_arr as $tmp_v) {
$html .= '<tr><td style="background:#90EE90" class="c1">'.$tmp_v['title'].' : ' . $tmp_v ['Key_name'] . '</td>';
$html .= '<td style="background:#90EE90" class="c6"> ' . $tmp_v ['Column_name'] . '</td>';
$html .= '<td style="background:#90EE90" class="c2">' . $tmp_v ['Index_type'] . '</td>';
$html .= '</tr>';
}
$html .= '</tbody></table>';
}
mysqli_close ( $mysql_conn );
// $html.= '</div>'; // 适应手机的代码
$mulu .= '</ul></div>';
// 输出
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>' . $title . '</title>
<style>
body,td,th {font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#6089D4;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:25px; font-size:16px; border:3px solid #fff; color:#ffffff; padding:5px;}
table td{height:25px; font-size:12px; border:3px solid #fff; background-color:#f0f0f0; padding:5px;}
.c1{ width: 150px;}
.c2{ width: 130px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 300px;}
</style>
</head>
<body>';
echo '<h1 style="text-align:center;">' . $title . '</h1>';
echo $mulu;
echo $html;
echo '</body></html>';