mysql生成数据字典程序

<?php
   
/**
 * 生成mysql数据字典
 */
       
//配置数据库
$dbserver   = "host:port";
$dbusername = "username";
$dbpassword = "password";
$database   = $_GET['db'] ? $_GET['db'] : "default_db";
   
   
//其他配置
$title = $database."_structure";
       
$mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
mysql_select_db($database, $mysql_conn);
mysql_query('SET NAMES utf8', $mysql_conn);
$table_result = mysql_query('show tables', $mysql_conn);
   
$tables_nav_html = '';
$databases_nav_html = '';
   
//取得所有的表名
while ($row = mysql_fetch_array($table_result)) {
    $tables[]['TABLE_NAME'] = $row[0];
    if(strpos($row[0],'deprecated')!==false){
        $tables_nav_html .= '<li><s><a style="color:gray;" href="#'. $row[0] .'">'. $row[0] .'</a></s></li>';
    }else{
        $tables_nav_html .= '<li><a style="color:red;" href="#'. $row[0] .'">'. $row[0] .'</a></li>';
    }
}
   
//取得所有的数据库
$database_result = mysql_query('show databases like '%jy_%'', $mysql_conn);
while ($row = mysql_fetch_array($database_result)) {
    $databases_nav_html .= '<li><a href="?db='. $row[0] .'">'. $row[0] .'</a></li>';
}
   
/* 输出所有表弹出层 */
echo '
<script type="text/javascript" src="http://demo.jb51.net/jslib/jquery/jquery-1.7.2.min.js"></script> 
<script type="text/javascript"> 
$(function() { 
   
$(window).scroll(function() { 
    var top = $(window).scrollTop()+10; 
    var left= $(window).scrollLeft()+10; 
    $("#databaseNav").css({ left:left + "px", top: top + "px" }); 
});
   
$(window).scroll(function() { 
    var top = $(window).scrollTop()+20; 
    var left= $(window).scrollLeft()+1300; 
    $("#tableNav").css({ left:left + "px", top: top + "px" }); 
}); 
   
   
$(window).ready(function() { 
    var top = $(window).scrollTop()+20; 
    var left= $(window).scrollLeft()+1300; 
    $("#tableNav").css({ left:left + "px", top: top + "px" }); 
}); 
   
}); 
</script> 
';
   
echo '<div id="tableNav" style="float:left;width:auto;background-color:#efefef;position:absolute;top:20px;border: 1px solid rgb(204, 204, 204);padding: 5px;line-height: 18px;">数据表:<br/>';
echo '<ul>'. $tables_nav_html .'</ul></div>';
   
/* 输出数据库弹出层  */
echo '
<div id="databaseNav" style="float:right;width:auto;background-color:#efefef;position:absolute;top:10px;border: 1px solid rgb(204, 204, 204);padding: 5px;line-height: 18px;">
数据库:<br/>';
echo '<ul>'. $databases_nav_html .'</ul></div>';
       
//循环取得所有表的备注及表中列消息
foreach ($tables AS $k=>$v) {
    $sql  = 'SELECT * FROM ';
    $sql .= 'INFORMATION_SCHEMA.TABLES ';
    $sql .= 'WHERE ';
    $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
    $table_result = mysql_query($sql, $mysql_conn);
    while ($t = mysql_fetch_array($table_result) ) {
        $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
    }
       
    $sql  = 'SELECT * FROM ';
    $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
    $sql .= 'WHERE ';
    $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
       
    $fields = array();
    $field_result = mysql_query($sql, $mysql_conn);
    while ($t = mysql_fetch_array($field_result) ) {
        $fields[] = $t;
    }
    $tables[$k]['COLUMN'] = $fields;
}
mysql_close($mysql_conn);
       
       
$html = '';
//循环所有表
foreach ($tables AS $k=>$v) {
    $html .= '<a name="'. $v['TABLE_NAME'] .'" id="'. $v['TABLE_NAME'] .'" ></a>';
    if(strpos($v['TABLE_NAME'],'deprecated')!==false){
        $html .= '<table border="1" cellspacing="0" width="800" cellpadding="0" align="center"><caption><s>`' . $v['TABLE_NAME'] .'`</s>  '. $v['TABLE_COMMENT']. '(已废弃)</caption><tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
    <th>允许非空</th>
    <th>自动递增</th><th>备注</th></tr><tr>';
    }else{
        $html .= '<table border="1" cellspacing="0" width="800" cellpadding="0" align="center"><caption>`' . $v['TABLE_NAME'] .'`  '. $v['TABLE_COMMENT']. '</caption><tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
    <th>允许非空</th>
    <th>自动递增</th><th>备注</th></tr><tr>'; 
    }
    foreach ($v['COLUMN'] AS $f) {
        $html .= '<td class="c1">' . $f['COLUMN_NAME'] . '</td><td class="c2">' . $f['COLUMN_TYPE'] . '</td><td class="c3"> ' . $f['COLUMN_DEFAULT'] . '</td><td class="c4"> ' . $f['IS_NULLABLE'] . '</td><td class="c5">' . ($f['EXTRA']=='auto_increment'?'是':' ') . '</td><td class="c6"> ' . $f['COLUMN_COMMENT'] . '</td></tr>';
            
    }
    $html .= '</tbody></table><br/><br/>';
}
       
//输出
echo '
       
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>'.$title.'</title>
<style>
body,td,th {font-family:"宋体"; font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
.c1{ width: 120px;}
.c2{ width: 120px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 270px;}
</style>
       
';
echo '<h1 style="text-align:center;">'.$title.'</h1>';
echo $html;
?>

效果:


jy_pc_common_structure

`article`
字段名数据类型默认值允许非空自动递增备注
idint(11)
NO
platformint(11)
NO
平台
typetinyint(1)
NO
1为普通文章,2为推广技巧,3.公告列表,4.常见问题,5.数据动态,6.帮助中心
titlevarchar(100)
NO
标题
contentlongtext
NO
内容
importanttinyint(1)
NO
是否重要,0:不重要 1:重要
is_toptinyint(4)0NO
是否置顶,0:不置顶 1:置顶
statustinyint(1)
NO
是否显示,0:不显示 1:显示
addtimedatetime
NO
添加时间
system_statustinyint(1)1YES
系统文章,禁止删除


`bank`
字段名数据类型默认值允许非空自动递增备注
idint(11)
NO
namevarchar(20)
NO


by 雪洁 2015-05-18 08:22:15 1348 views
我来说几句

相关文章