[轉載]PHP匯出資料庫資料字典指令碼

來源:互聯網
上載者:User
 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>';
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.