標籤:char cells sql set oca while header inf images
平時做開發時,查看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‘] . ‘ </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"> ‘ . $f [‘COLUMN_DEFAULT‘] . ‘</td>‘;65 $html .= ‘<td class="c4"> ‘ . $f [‘IS_NULLABLE‘] . ‘</td>‘;66 $html .= ‘<td class="c5">‘ . ($f [‘EXTRA‘] == ‘auto_increment‘ ? ‘是‘ : ‘ ‘) . ‘</td>‘;67 $html .= ‘<td class="c6"> ‘ . $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資料字典代碼