1 <?php
 2 /**
 3  * 生成mysql数据字典
 4  *
 5  * @version $id$
 6  * @author niming<287384795@qq.com>
 7  */
 8 
 9 //配置数据库
10 $dbserver   = "localhost";
11 $dbusername = "root";
12 $dbpassword = "root";
13 $database   = 'mydb';
14 //其他配置
15 $title = '系统数据字典';
16 
17 $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
18 mysql_select_db($database, $mysql_conn);
19 mysql_query('SET NAMES utf8', $mysql_conn);
20 $table_result = mysql_query('show tables', $mysql_conn);
21 //取得所有的表名
22 while ($row = mysql_fetch_array($table_result)) {
23     $tables[]['TABLE_NAME'] = $row[0];
24 }
25 
26 //循环取得所有表的备注
27 foreach ($tables AS $k=>$v) {
28     $sql  = 'SELECT * FROM ';
29     $sql .= 'INFORMATION_SCHEMA.TABLES ';
30     $sql .= 'WHERE ';
31     $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
32     $table_result = mysql_query($sql, $mysql_conn);
33     while ($t = mysql_fetch_array($table_result) ) {
34         $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
35     }
36 
37     $sql  = 'SELECT * FROM ';
38     $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
39     $sql .= 'WHERE ';
40     $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
41 
42     $fields = array();
43     $field_result = mysql_query($sql, $mysql_conn);
44     while ($t = mysql_fetch_array($field_result) ) {
45         $fields[] = $t;
46     }
47     $tables[$k]['COLUMN'] = $fields;
48 }
49 mysql_close($mysql_conn);
50 
51 
52 $html = '';
53 //循环所有表
54 foreach ($tables AS $k=>$v) {
55     $html .= '<p><h2>'. $v['TABLE_COMMENT'] . '&nbsp;</h2>';
56     $html .= '<table  border="1" cellspacing="0" cellpadding="0" align="center">';
57     $html .= '<caption>' . $v['TABLE_NAME'] . '</caption>';
58     $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
59     <th>允许非空</th>
60     <th>自动递增</th><th>备注</th></tr>';
61     $html .= '';
62 
63     foreach ($v['COLUMN'] AS $f) {
64         $html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
65         $html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
66         $html .= '<td class="c3">&nbsp;' . $f['COLUMN_DEFAULT'] . '</td>';
67         $html .= '<td class="c4">&nbsp;' . $f['IS_NULLABLE'] . '</td>';
68         $html .= '<td class="c5">' . ($f['EXTRA']=='auto_increment'?'是':'&nbsp;') . '</td>';
69         $html .= '<td class="c6">&nbsp;' . $f['COLUMN_COMMENT'] . '</td>';
70         $html .= '</tr>';
71     }
72     $html .= '</tbody></table></p>';
73 }
74 
75 
76 //输出
77 echo '<html>
78 <head>
79 <title>' . $title . '</title>
80 <style>
81 body,td,th {font-family:"宋体"; font-size:12px;}
82 table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
83 table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
84 table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;}
85 table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
86 .c1{ width: 120px;}
87 .c2{ width: 120px;}
88 .c3{ width: 70px;}
89 .c4{ width: 80px;}
90 .c5{ width: 80px;}
91 .c6{ width: 270px;}
92 </style>
93 </head>
94 <body>';
95 echo '<h1>' . $title . '</h1>';
96 echo $html;
97 echo '</body></html>';
posted on 2012-06-04 06:51  ZimZz  阅读(2005)  评论(0编辑  收藏  举报