项目地址

在线预览

可以快速查看数据库中各表的字段名/字段类型/默认值及描述

可以快速查看各字段是否有索引及索引名称,类型等相关信息

所需环境:

  • php 5.6以上
  • mysql 5.1及以上
  • nginx

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

$title = " <tr>
<td width=\"180\">字段名 </td>
<td width=\"160\">字段类型</td>
<td width=\"80\">可否为空</td>

<td width=\"80\">Key</td>
<td width=\"60\">默认值</td>
<td>字段描述</td>
</tr>";

$indexTitle = " <tr>
<td width=\"60\">COLUMN_NAME</td>

<td width=\"60\">NON_UNIQUE</td>
<td width=\"60\">INDEX_NAME</td>
<td width=\"60\">SEQ_IN_INDEX</td>

<td width=\"60\">COLLATION</td>
<td width=\"60\">CARDINALITY</td>

<td width=\"60\">SUB_PART</td>

<td width=\"60\">PACKED</td>

<td width=\"60\">NULLABLE</td>

<td width=\"60\">INDEX_TYPE</td>

</tr>";


const HOST = "";
const DBNAME = "";
const USER = "";
const PASSWORD = "";




$pdo = new PDO("mysql:host=118.126.97.71;dbname=daohang", USER, PASSWORD);
$pdo->query('set names utf8');
$pdo->query('use information_schema');


$allTablesSql = 'SELECT table_name name,TABLE_COMMENT value FROM INFORMATION_SCHEMA.TABLES WHERE table_type=\'base table\'
and table_schema = \'daohang\' order by table_name asc';
$query = $pdo->query($allTablesSql);

$rs = $query->fetchAll();


$allTablesRecordSql = 'select table_name,table_rows from tables where TABLE_SCHEMA = \'daohang\' order by table_rows desc; ';
$query2 = $pdo->query($allTablesRecordSql);
$rs2 = $query2->fetchAll();


//获取表名和记录数的对应关系
$recordArr = [];

foreach ($rs2 as $tableInfo) {

$tableName = $tableInfo["table_name"];
$tableRows = $tableInfo["table_rows"];
$recordArr[$tableName] = $tableRows;
}


//获取表名和索引的对应关系
$allTablesIndexSql = ' SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = \'daohang\'; ';
$query3 = $pdo->query($allTablesIndexSql);
$rs3 = $query3->fetchAll();



//获取表名和索引的对应关系,三维数组,键名是表名,键值是个二维数组,里面是这张表所有的索引
$indexArr = [];

foreach ($rs as $one) {
$tName = $one["name"];
$indexArr[$tName] = [];
}


foreach ($rs3 as $item) {
$tNameWithIndex = $item["TABLE_NAME"];
array_push($indexArr[$tNameWithIndex], $item);

}



$pdo->query('use daohang;');


$html = '';
foreach ($rs as $v) {
$sql = "show full columns from `$v[name]` ";

$query = $pdo->query($sql);
$tableData = $query->fetchAll();
$count = count($tableData);
if (empty($v['value'])) {
$v['value'] = '无';
}


$recordNum = $recordArr[$v['name']];


$head = "<table class=\"tablelist\" style=\"margin:20px 0 0 20px; width:1200px;\">
<tr><td colspan=\"10\" style=\"font-weight: bold; background:#abcbbb; \"> 表:[ $v[name] ] &nbsp &nbsp &nbsp &nbsp &nbsp 描述: &nbsp <font color='blue'>$v[value] </font> &nbsp &nbsp &nbsp &nbsp &nbsp 行数: &nbsp <font color='purple'>$recordNum </font> </td></tr>";


$indexHead = "<table class=\"tablelist\" style=\"margin:20px 0 0 20px; width: 1200px;\">
<tr><td height=\"3px\" colspan=\"10\" style=\"font-weight: bold; background:salmon; \"> <font color='purple' size='1px'>&nbsp;<a href='https://blog.csdn.net/itas109/article/details/82879397' target='_blank'>索引信息:</a> </font> </td></tr>";

$fenge = "<table class=\"tablelist\" style=\"margin:20px 0 0 20px; width: 1200px;\">
<tr><td height=\"3px\" colspan=\"10\" style=\"font-weight: bold; background:scroll; \"> <font color='#1e90ff' size='1px'>&nbsp;<HR SIZE=1> </font> </td></tr>"; //html画实线https://blog.csdn.net/lance_lot1/article/details/7921441


$str = '';
foreach ($tableData as $key => $item) {
$field = $item['Field'];
$type = $item['Type'];
$is_null = $item['Null'];

$key = empty($item['Key']) ? "&nbsp;- " : "<font color='#6a5acd'>" . $item['Key'] . "</font>";
$default = empty($item['Default']) ? '无' : "<font color='#daa520'>" . $item['Default'] . "</font>";
$comment = empty($item['Comment']) ? '无' : "<font color='red'>" . $item['Comment'] . "</font>";

$body = "<tr>
<td width=\"180\">$field</td>
<td width=\"160\">$type</td>
<td width=\"80\">$is_null</td>
<td width=\"80\">$key</td>
<td width=\"60\">$default</td>
<td>$comment</td>
</tr>";
$str .= $body; //字段信息(如类型,可否为空,备注等)
}


$tableIndex = $indexArr[$v["name"]]; //单张表的所有索引信息,二维数组

$indexStr = "";
foreach ($tableIndex as $itemIndex) {
$body = "<tr>
<td width=\"60\">$itemIndex[COLUMN_NAME]</td>
<td width=\"60\">$itemIndex[NON_UNIQUE]</td>
<td width=\"60\">$itemIndex[INDEX_NAME]</td>
<td width=\"60\">$itemIndex[SEQ_IN_INDEX]</td>
<td width=\"60\">$itemIndex[COLLATION]</td>
<td width=\"60\">$itemIndex[CARDINALITY]</td>
<td width=\"60\">$itemIndex[SUB_PART]</td>
<td width=\"60\">$itemIndex[PACKED]</td>
<td width=\"60\">$itemIndex[NULLABLE]</td>
<td width=\"60\">$itemIndex[INDEX_TYPE]</td>
</tr>";

$indexStr .= $body;

}

$html .= $head . $title . $str . $indexHead . $indexTitle . $indexStr . $fenge;

}

echo $html;