mysql数据字典代码
Posted 上一站牵手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据字典代码相关的知识,希望对你有一定的参考价值。
平时做开发时,查看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数据字典代码的主要内容,如果未能解决你的问题,请参考以下文章