Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. <?php
  2. /**
  3. * 生成mysql数据字典
  4. */
  5. header("Content-type: text/html; charset=utf-8");
  6. require_once('../config.php');
  7. //配置数据库
  8. $dbserver = $_CFG['mysql_host'];
  9. $dbusername = $_CFG['mysql_user'];
  10. $dbpassword = $_CFG['mysql_pwd'];
  11. $database = $_CFG['mysql_db'];
  12.  
  13. try {
  14. $mysql_conn = new PDO('mysql:host='.$dbserver.';dbname='.$database, $dbusername, $dbpassword, [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "UTF8"' ]);
  15. } catch (PDOException $e) {
  16. echo 'Connection failed: '.$e->getMessage();
  17. }
  18.  
  19. //其他配置
  20. $sql = 'show tables';
  21. $statement = $mysql_conn->prepare($sql);
  22. $statement->execute();
  23. $table_result = $statement->fetchAll(PDO::FETCH_NUM);
  24.  
  25. $no_show_table = array(); //不需要显示的表
  26. $no_show_field = array(); //不需要显示的字段
  27.  
  28. //取得所有的表名
  29. $tables = array();
  30. foreach($table_result as $value) {
  31. if(!in_array($value[0],$no_show_table)){
  32. $tables[]['TABLE_NAME'] = $value[0];
  33. }
  34. }
  35.  
  36. //替换所有表的表前缀
  37. if(!empty($_GET['prefix'])){
  38. $prefix = 'czzj';
  39. foreach($tables as $key => $val){
  40. $tableName = $val['TABLE_NAME'];
  41. $string = explode('_',$tableName);
  42. if($string[0] != $prefix){
  43. $string[0] = $prefix;
  44. $newTableName = implode('_', $string);
  45. $sql = 'rename table '.$tableName.' TO '.$newTableName;
  46. $statement = $mysql_conn->prepare($sql);
  47. $statement->execute();
  48. }
  49. }
  50. echo "替换成功!";exit();
  51. }
  52.  
  53. //循环取得所有表的备注及表中列消息
  54. foreach ($tables as $k=>$v) {
  55. $sql = 'SELECT * FROM ';
  56. $sql .= 'INFORMATION_SCHEMA.TABLES ';
  57. $sql .= 'WHERE ';
  58. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  59. $statement = $mysql_conn->prepare($sql);
  60. $statement->execute();
  61.  
  62. $t = $statement->fetchALL(PDO::FETCH_ASSOC);
  63. $tables[$k]['TABLE_COMMENT'] = $t[0]['TABLE_COMMENT'];
  64.  
  65. $sql = 'SELECT * FROM ';
  66. $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
  67. $sql .= 'WHERE ';
  68. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  69.  
  70. $fields = array();
  71. $statement = $mysql_conn->prepare($sql);
  72. $statement->execute();
  73. $result = $statement->fetchAll(PDO::FETCH_ASSOC);
  74. foreach($result as $item) {
  75. $fields[] = $item;
  76. }
  77.  
  78. $tables[$k]['COLUMN'] = $fields;
  79. }
  80.  
  81. $html = '';
  82. //循环所有表
  83. foreach ($tables as $k=>$v) {
  84. if(!in_array($v['TABLE_NAME'][0], $no_show_table)){
  85. $html .= ' <h3>' . ($k + 1) . '、' .$v['TABLE_COMMENT'].' ('. $v['TABLE_NAME']. ')</h3>'."\n";
  86. $html .= ' <table border="1" cellspacing="0" cellpadding="0" width="100%">'."\n";
  87. $html .= ' <tbody>'."\n";
  88. $html .= ' <tr>'."\n";
  89. $html .= ' <th>字段名</th>'."\n";
  90. $html .= ' <th>数据类型</th>'."\n";
  91. $html .= ' <th>默认值</th>'."\n";
  92. $html .= ' <th>允许非空</th>'."\n";
  93. $html .= ' <th>自动递增</th>'."\n";
  94. $html .= ' <th>备注</th>'."\n";
  95. $html .= ' </tr>'."\n";
  96.  
  97. foreach ($v['COLUMN'] as $f) {
  98. if(!in_array($f['COLUMN_NAME'],$no_show_field)){
  99. $html .= ' <tr>'."\n";
  100. $html .= ' <td class="c1">' . $f['COLUMN_NAME'] . '</td>'."\n";
  101. $html .= ' <td class="c2">' . $f['COLUMN_TYPE'] . '</td>'."\n";
  102. $html .= ' <td class="c3">' . $f['COLUMN_DEFAULT'] . '</td>'."\n";
  103. $html .= ' <td class="c4">' . $f['IS_NULLABLE'] . '</td>'."\n";
  104. $html .= ' <td class="c5">' . ($f['EXTRA']=='auto_increment'?'是':'&nbsp;') . '</td>'."\n";
  105. $html .= ' <td class="c6">' . $f['COLUMN_COMMENT'] . '</td>'."\n";
  106. $html .= ' </tr>'."\n";
  107. }
  108. }
  109. $html .= ' </tbody>'."\n";
  110. $html .= ' </table>'."\n";
  111. }
  112. }
  113. ?>
  114. <!doctype html>
  115. <html>
  116. <head>
  117. <meta charset="utf-8">
  118. <title><?php echo $_CFG['mysql_dbName'];?> | 数据字典</title>
  119. <meta name="generator" content="ThinkDb V1.0" />
  120. <meta name="author" content="" />
  121. <meta name="copyright" content="2008-2014 Tensent Inc." />
  122. <style>
  123. body, td, th { font-family: "微软雅黑"; font-size: 14px; }
  124. .warp{margin:auto; width:900px;}
  125. .warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;}
  126. table { border-collapse: collapse; border: 1px solid #CCC; background: #efefef; }
  127. 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;}
  128. table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;}
  129. .c1 { width: 120px; }
  130. .c2 { width: 120px; }
  131. .c3 { width: 150px; }
  132. .c4 { width: 80px; text-align:center;}
  133. .c5 { width: 80px; text-align:center;}
  134. .c6 { width: 270px; }
  135. </style>
  136. </head>
  137. <body>
  138. <div class="warp">
  139. <h1 style="text-align:center;"><?php echo $_CFG['mysql_dbName'];?> | 数据字典</h1>
  140. <?php echo $html; ?>
  141. </div>
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement