1. 程式人生 > >mysql數據字典代碼

mysql數據字典代碼

fields res add 顯示效果 ica mysq 生成 font utf

平時做開發時,查看mysql的表是用了navicat,雖然查看單個表的各個字段時還算方便,但是要一次查看整個數據庫各個表的各個字段的詳情還是不那麽方便,於是就在網上找了一段代碼,把數據庫的所有表的字段都顯示出來,這樣查看結構會比較清晰。

顯示效果

技術分享

 1 <?php
 2 /**
 3  * 生成mysql數據字典
 4  */
 5 header ( "Content-type: text/html; charset=utf-8" );
 6  
 7 // 配置數據庫
 8 $dbserver = "localhost";//數據庫IP地址
 9 $dbusername = "root";//數據庫用戶名
10 $dbpassword = "";//數據庫密碼 11 $database = "test";//數據庫名稱 12 13 // 其他配置 14 $title = ‘數據字典‘; 15 16 $mysql_conn = @mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." ); 17 mysql_select_db ( $database, $mysql_conn ); 18 mysql_query ( ‘SET NAMES utf8‘, $mysql_conn
); 19 $table_result = mysql_query ( ‘show tables‘, $mysql_conn ); 20 // 取得所有的表名 21 while ( $row = mysql_fetch_array ( $table_result ) ) { 22 $tables [] [‘TABLE_NAME‘] = $row [0]; 23 } 24 25 // 循環取得所有表的備註及表中列消息 26 foreach ( $tables as $k => $v ) { 27 $sql = ‘SELECT * FROM ‘; 28 $sql .= ‘INFORMATION_SCHEMA.TABLES ‘;
29 $sql .= ‘WHERE ‘; 30 $sql .= "table_name = ‘{$v[‘TABLE_NAME‘]}‘ AND table_schema = ‘{$database}‘"; 31 $table_result = mysql_query ( $sql, $mysql_conn ); 32 while ( $t = mysql_fetch_array ( $table_result ) ) { 33 $tables [$k] [‘TABLE_COMMENT‘] = $t [‘TABLE_COMMENT‘]; 34 } 35 36 $sql = ‘SELECT * FROM ‘; 37 $sql .= ‘INFORMATION_SCHEMA.COLUMNS ‘; 38 $sql .= ‘WHERE ‘; 39 $sql .= "table_name = ‘{$v[‘TABLE_NAME‘]}‘ AND table_schema = ‘{$database}‘"; 40 41 $fields = array (); 42 $field_result = mysql_query ( $sql, $mysql_conn ); 43 while ( $t = mysql_fetch_array ( $field_result ) ) { 44 $fields [] = $t; 45 } 46 $tables [$k] [‘COLUMN‘] = $fields; 47 } 48 mysql_close ( $mysql_conn ); 49 50 $html = ‘‘; 51 // 循環所有表 52 foreach ( $tables as $k => $v ) { 53 // $html .= ‘<p><h2>‘. $v[‘TABLE_COMMENT‘] . ‘&nbsp;</h2>‘; 54 $html .= ‘<table border="1" cellspacing="0" cellpadding="0" align="center">‘; 55 $html .= ‘<caption>‘ . $v [‘TABLE_NAME‘] . ‘ ‘ . $v [‘TABLE_COMMENT‘] . ‘</caption>‘; 56 $html .= ‘<tbody><tr><th>字段名</th><th>數據類型</th><th>默認值</th> 57 <th>允許非空</th> 58 <th>自動遞增</th><th>備註</th></tr>‘; 59 $html .= ‘‘; 60 61 foreach ( $v [‘COLUMN‘] as $f ) { 62 $html .= ‘<tr><td class="c1">‘ . $f [‘COLUMN_NAME‘] . ‘</td>‘; 63 $html .= ‘<td class="c2">‘ . $f [‘COLUMN_TYPE‘] . ‘</td>‘; 64 $html .= ‘<td class="c3">&nbsp;‘ . $f [‘COLUMN_DEFAULT‘] . ‘</td>‘; 65 $html .= ‘<td class="c4">&nbsp;‘ . $f [‘IS_NULLABLE‘] . ‘</td>‘; 66 $html .= ‘<td class="c5">‘ . ($f [‘EXTRA‘] == ‘auto_increment‘ ? ‘是‘ : ‘&nbsp;‘) . ‘</td>‘; 67 $html .= ‘<td class="c6">&nbsp;‘ . $f [‘COLUMN_COMMENT‘] . ‘</td>‘; 68 $html .= ‘</tr>‘; 69 } 70 $html .= ‘</tbody></table></p>‘; 71 } 72 73 // 輸出 74 echo<html> 75 <head> 76 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 77 <title>‘ . $title . ‘</title> 78 <style> 79 body,td,th {font-family:"宋體"; font-size:12px;} 80 table{border-collapse:collapse;border:1px solid #CCC;background:#6089D4;} 81 table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; } 82 table th{text-align:left; font-weight:bold;height:26px; line-height:25px; font-size:16px; border:3px solid #fff; color:#ffffff; padding:5px;} 83 table td{height:25px; font-size:12px; border:3px solid #fff; background-color:#f0f0f0; padding:5px;} 84 .c1{ width: 150px;} 85 .c2{ width: 130px;} 86 .c3{ width: 70px;} 87 .c4{ width: 80px;} 88 .c5{ width: 80px;} 89 .c6{ width: 300px;} 90 </style> 91 </head> 92 <body>‘; 93 echo ‘<h1 style="text-align:center;">‘ . $title . ‘</h1>‘; 94 echo $html; 95 echo ‘</body></html>‘; 96 97 ?>

mysql數據字典代碼