php 导出Mysql 数据字典

使用方法:先配置好数据库链接与要导出的数据库名,然后运行。

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

 

转载于:https://www.cnblogs.com/banyanCheung/archive/2013/02/22/2994430.html

    原文作者:baicheng6127
    原文地址: https://blog.csdn.net/baicheng6127/article/details/101402279
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞