免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2713 | 回复: 1
打印 上一主题 下一主题

遍历数据库结构 [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-07-10 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-09 11:02 |只看该作者 |倒序浏览
[PHP]代码
  1. <?php
  2. /**
  3. * 生成mysql数据字典
  4. */
  5. header("Content-type: text/html; charset=utf-8");
  6. //配置数据库
  7. $dbserver   = "localhost";
  8. $dbusername = "root";
  9. $dbpassword = "";
  10. $database   = "fw";
  11.   
  12. //其他配置
  13. $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
  14. mysql_select_db($database, $mysql_conn);
  15. mysql_query('SET NAMES UTF8', $mysql_conn);
  16. $table_result = mysql_query('show tables', $mysql_conn);
  17.   
  18. $no_show_table = array();    //不需要显示的表
  19. $no_show_field = array();   //不需要显示的字段
  20.   
  21. //取得所有的表名
  22. while($row = mysql_fetch_array($table_result)){
  23.     if(!in_array($row[0],$no_show_table)){
  24.         $tables[]['TABLE_NAME'] = $row[0];
  25.     }
  26. }
  27. //替换所以表的表前缀
  28. if($_GET['prefix']){
  29.     $prefix = 'ych';
  30.     foreach($tables as $key => $val){
  31.         $tableName = $val['TABLE_NAME'];
  32.         $string = explode('_',$tableName);
  33.         if($string[0] != $prefix){  
  34.             $string[0] = $prefix;  
  35.             $newTableName = implode('_', $string);  
  36.             mysql_query('rename table '.$tableName.' TO '.$newTableName);  
  37.         }
  38.     }
  39.     echo "替换成功!";exit();
  40. }
  41.   
  42. //循环取得所有表的备注及表中列消息
  43. foreach ($tables as $k=>$v) {
  44.     $sql  = 'SELECT * FROM ';
  45.     $sql .= 'INFORMATION_SCHEMA.TABLES ';
  46.     $sql .= 'WHERE ';
  47.     $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
  48.     $table_result = mysql_query($sql, $mysql_conn);
  49.     while ($t = mysql_fetch_array($table_result) ) {
  50.         $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
  51.     }
  52.   
  53.     $sql  = 'SELECT * FROM ';
  54.     $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
  55.     $sql .= 'WHERE ';
  56.     $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  57.   
  58.     $fields = array();
  59.     $field_result = mysql_query($sql, $mysql_conn);
  60.     while ($t = mysql_fetch_array($field_result) ) {
  61.         $fields[] = $t;
  62.     }
  63.     $tables[$k]['COLUMN'] = $fields;
  64. }
  65. mysql_close($mysql_conn);
  66.   
  67.   
  68. $html = '';
  69. //循环所有表
  70. foreach ($tables as $k=>$v) {
  71.     $html .= '  <h3>' . ($k + 1) . '、' . $v['TABLE_COMMENT'] .'  ('. $v['TABLE_NAME']. ')</h3>'."\n";
  72.     $html .= '  <table border="1" cellspacing="0" cellpadding="0" width="100%">'."\n";
  73.     $html .= '      <tbody>'."\n";
  74.     $html .= '          <tr>'."\n";
  75.     $html .= '              <th>字段名</th>'."\n";
  76.     $html .= '              <th>数据类型</th>'."\n";
  77.     $html .= '              <th>默认值</th>'."\n";
  78.     $html .= '              <th>允许非空</th>'."\n";
  79.     $html .= '              <th>自动递增</th>'."\n";
  80.     $html .= '              <th>备注</th>'."\n";
  81.     $html .= '          </tr>'."\n";
  82.   
  83.     foreach ($v['COLUMN'] as $f) {
  84.         if(!is_array($no_show_field[$v['TABLE_NAME']])){
  85.             $no_show_field[$v['TABLE_NAME']] = array();
  86.         }
  87.         if(!in_array($f['COLUMN_NAME'],$no_show_field[$v['TABLE_NAME']])){
  88.             $html .= '          <tr>'."\n";
  89.             $html .= '              <td class="c1">' . $f['COLUMN_NAME'] . '</td>'."\n";
  90.             $html .= '              <td class="c2">' . $f['COLUMN_TYPE'] . '</td>'."\n";
  91.             $html .= '              <td class="c3">' . $f['COLUMN_DEFAULT'] . '</td>'."\n";
  92.             $html .= '              <td class="c4">' . $f['IS_NULLABLE'] . '</td>'."\n";
  93.             $html .= '              <td class="c5">' . ($f['EXTRA']=='auto_increment'?'是':'&nbsp;') . '</td>'."\n";
  94.             $html .= '              <td class="c6">' . $f['COLUMN_COMMENT'] . '</td>'."\n";
  95.             $html .= '          </tr>'."\n";
  96.         }
  97.     }
  98.     $html .= '      </tbody>'."\n";
  99.     $html .= '  </table>'."\n";
  100. }
  101. ?>
  102. <!doctype html>
  103. <html>
  104. <head>
  105. <meta charset="utf-8">
  106. <title>数据库数据字典</title>
  107. <style>
  108. body, td, th { font-family: "微软雅黑"; font-size: 14px; }
  109. .warp{margin:auto; width:900px;}
  110. .warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;}
  111. table { border-collapse: collapse; border: 1px solid #CCC; background: #efefef; }
  112. table th { text-align: left; font-weight: bold; height: 26px; line-height: 26px; font-size: 14px; text-align:center; border: 1px solid #CCC; padding:5px;}
  113. table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;}
  114. .c1 { width: 120px; }
  115. .c2 { width: 120px; }
  116. .c3 { width: 150px; }
  117. .c4 { width: 80px; text-align:center;}
  118. .c5 { width: 80px; text-align:center;}
  119. .c6 { width: 270px; }
  120. </style>
  121. </head>
  122. <body>
  123. <div class="warp">
  124. <?php echo $html; ?>
  125. </div>
  126. </body>
  127. </html>
复制代码

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
2 [报告]
发表于 2015-07-10 09:52 |只看该作者
赞!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP