Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  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 = "auto";
  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 = 'czzj';
  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. <h1 style="text-align:center;">数据库数据字典生成代码</h1>
  125. <?php echo $html; ?>
  126. </div>
  127. </body>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement