PHP生成MYSQL 数据字典

本文阅读 1 分钟
首页 PHP笔记 正文
  1. <?php
  2. /**
  3. * 生成mysql数据字典
  4. */
  5. header("Content-type: text/html; charset=utf-8");
  6. //配置数据库
  7. $dbserver = "127.0.0.1";
  8. $dbusername = "root";
  9. $dbpassword = "root";
  10. $database = "posp";
  11. //其他配置
  12. $mysql&nbsp;=&nbsp;new&nbsp;MySQLi(&quot;$dbserver", "$dbusername&quot;,&nbsp;&quot;$dbpassword", "$database") or die("Mysql connect is error.");
  13. $mysql -> set_charset('utf8');
  14. $table_result&nbsp;=&nbsp;$mysql->query('show tables');
  15. $no_show_table = array(); //不需要显示的表
  16. $no_show_field = array(); //不需要显示的字段
  17. //取得所有的表名
  18. while($row&nbsp;=&nbsp;mysqli_fetch_array($table_result)){
  19. if(!in_array($row[0],$no_show_table)){
  20. $tables[][&#39;TABLE_NAME&#39;]&nbsp;=&nbsp;$row[0];
  21. }
  22. }
  23. //替换所以表的表前缀
  24. // if($_GET['prefix']){
  25. // $prefix = 'sent_';
  26. // foreach($tables&nbsp;as&nbsp;$key => $val){
  27. // $tableName&nbsp;=&nbsp;$val['TABLE_NAME'];
  28. // $string&nbsp;=&nbsp;explode(&#39;_&#39;,$tableName);
  29. // if($string[0]&nbsp;!=&nbsp;$prefix){
  30. // $string[0]&nbsp;=&nbsp;$prefix;
  31. // $newTableName&nbsp;=&nbsp;implode(&#39;_&#39;,&nbsp;$string);
  32. // $mysql-&gt;query(&#39;rename&nbsp;table&nbsp;&#39;.$tableName.' TO '.$newTableName);
  33. // }
  34. // }
  35. // echo "替换成功!";exit();
  36. // }
  37. //循环取得所有表的备注及表中列消息
  38. foreach ($tables&nbsp;as&nbsp;$k=>$v) {
  39. $sql = 'SELECT * FROM ';
  40. $sql .= 'INFORMATION_SCHEMA.TABLES ';
  41. $sql .= 'WHERE ';
  42. $sql&nbsp;.=&nbsp;&quot;table_name&nbsp;=&nbsp;&#39;{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  43. $table_result&nbsp;=&nbsp;$mysql->query($sql,&nbsp;$mysql_conn);
  44. while ($t&nbsp;=&nbsp;mysqli_fetch_array($table_result) ) {
  45. $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
  46. }
  47. $sql = 'SELECT * FROM ';
  48. $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
  49. $sql .= 'WHERE ';
  50. $sql&nbsp;.=&nbsp;&quot;table_name&nbsp;=&nbsp;&#39;{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  51. $fields = array();
  52. $field_result&nbsp;=&nbsp;$mysql->query($sql,&nbsp;$mysql_conn);
  53. while ($t&nbsp;=&nbsp;mysqli_fetch_array($field_result) ) {
  54. $fields[]&nbsp;=&nbsp;$t;
  55. }
  56. $tables[$k]['COLUMN'] = $fields;
  57. }
  58. $mysql-&gt;close($mysql_conn);
  59. $html = '';
  60. //循环所有表
  61. foreach ($tables&nbsp;as&nbsp;$k=>$v) {
  62. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&lt;h3&gt;&#39;&nbsp;.&nbsp;($k + 1) . '、' . $v[&#39;TABLE_COMMENT&#39;]&nbsp;.&#39;&nbsp;&nbsp;(&#39;.&nbsp;$v['TABLE_NAME']. ')</h3>'."n";
  63. $html .= ' <table border="1" cellspacing="0" cellpadding="0" width="100%">'."n";
  64. $html .= ' <tbody>'."n";
  65. $html .= ' <tr>'."n";
  66. $html .= ' <th>字段名</th>'."n";
  67. $html .= ' <th>数据类型</th>'."n";
  68. $html .= ' <th>默认值</th>'."n";
  69. $html .= ' <th>允许非空</th>'."n";
  70. $html .= ' <th>自动递增</th>'."n";
  71. $html .= ' <th>备注</th>'."n";
  72. $html .= ' </tr>'."n";
  73. foreach ($v[&#39;COLUMN&#39;]&nbsp;as&nbsp;$f) {
  74. if(!@is_array($no_show_field[$v['TABLE_NAME']])){
  75. $no_show_field[$v['TABLE_NAME']] = array();
  76. }
  77. if(!in_array($f[&#39;COLUMN_NAME&#39;],$no_show_field[$v['TABLE_NAME']])){
  78. $html .= ' <tr>'."n";
  79. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c1&quot;&gt;&#39;&nbsp;.&nbsp;$f['COLUMN_NAME'] . '</td>'."n";
  80. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c2&quot;&gt;&#39;&nbsp;.&nbsp;$f['COLUMN_TYPE'] . '</td>'."n";
  81. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c3&quot;&gt;&#39;&nbsp;.&nbsp;$f['COLUMN_DEFAULT'] . '</td>'."n";
  82. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c4&quot;&gt;&#39;&nbsp;.&nbsp;$f['IS_NULLABLE'] . '</td>'."n";
  83. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c5&quot;&gt;&#39;&nbsp;.&nbsp;($f['EXTRA']=='auto_increment'?'是':'&nbsp;') . '</td>'."n";
  84. $html&nbsp;.=&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;class=&quot;c6&quot;&gt;&#39;&nbsp;.&nbsp;$f['COLUMN_COMMENT'] . '</td>'."n";
  85. $html .= ' </tr>'."n";
  86. }
  87. }
  88. $html .= ' </tbody>'."n";
  89. $html .= ' </table>'."n";
  90. }
  91. ?>
  92. <!doctype html>
  93. <html>
  94. <head>
  95. <meta charset="utf-8">
  96. <title>数据库数据字典</title>
  97. <style>
  98. body, td, th { font-family: "微软雅黑"; font-size: 14px; }
  99. .warp{margin:auto; width:900px;}
  100. .warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;}
  101. table { border-collapse: collapse; border: 1px solid #CCC; background: #efefef; }
  102. 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;}
  103. table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;}
  104. .c1 { width: 120px; }
  105. .c2 { width: 120px; }
  106. .c3 { width: 150px; }
  107. .c4 { width: 80px; text-align:center;}
  108. .c5 { width: 80px; text-align:center;}
  109. .c6 { width: 270px; }
  110. </style>
  111. </head>
  112. <body>
  113. <div class="warp">
  114. <h1 style="text-align:center;">数据库数据字典</h1>
  115. <?php echo $html; ?>
  116. </div>
  117. </body>
  118. </html>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP 获取每个月的开始和结束时间
« 上一篇 03-23
[Chrome]PC端浏览器弹窗广告屏蔽插件-adblock plus
下一篇 » 03-31

发表评论

惪特博客
  • 文章总数:
    18497 篇
  • 评论总数:
    53318 条
  • 标签总数:
    8873 个
  • 总浏览量:
    22605467 次
  • 最后更新:
    3天前

最多点赞

随便看看

标签TAG