1. * 生成mysql数据字典
  2. */
  3. header("Content-type: text/html; charset=utf-8");
  4. //配置数据库
  5. $dbserver = "127.0.0.1";
  6. $dbusername = "root";
  7. $dbpassword = "";
  8. $database = "think_team";
  9. //其他配置
  10. $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
  11. mysql_select_db($database, $mysql_conn);
  12. mysql_query('SET NAMES utf8', $mysql_conn);
  13. $table_result = mysql_query('show tables', $mysql_conn);
  14. $no_show_table = array(); //不需要显示的表
  15. $no_show_field = array(); //不需要显示的字段
  16. //取得所有的表名
  17. while($row = mysql_fetch_array($table_result)){
  18. if(!in_array($row[0],$no_show_table)){
  19. $tables[]['TABLE_NAME'] = $row[0];
  20. }
  21. }
  22. //替换所以表的表前缀
  23. if($_GET['prefix']){
  24. $prefix = 'czzj';
  25. foreach($tables as $key => $val){
  26. $tableName = $val['TABLE_NAME'];
  27. $string = explode('_',$tableName);
  28. if($string[0] != $prefix){
  29. $string[0] = $prefix;
  30. $newTableName = implode('_', $string);
  31. mysql_query('rename table '.$tableName.' TO '.$newTableName);
  32. }
  33. }
  34. echo "替换成功!";exit();
  35. }
  36. //循环取得所有表的备注及表中列消息
  37. foreach ($tables as $k=>$v) {
  38. $sql = 'SELECT * FROM ';
  39. $sql .= 'INFORMATION_SCHEMA.TABLES ';
  40. $sql .= 'WHERE ';
  41. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  42. $table_result = mysql_query($sql, $mysql_conn);
  43. while ($t = mysql_fetch_array($table_result) ) {
  44. $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
  45. }
  46. $sql = 'SELECT * FROM ';
  47. $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
  48. $sql .= 'WHERE ';
  49. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  50. $fields = array();
  51. $field_result = mysql_query($sql, $mysql_conn);
  52. while ($t = mysql_fetch_array($field_result) ) {
  53. $fields[] = $t;
  54. }
  55. $tables[$k]['COLUMN'] = $fields;
  56. }
  57. mysql_close($mysql_conn);
  58. $html = '';
  59. //循环所有表
  60. foreach ($tables as $k=>$v) {
  61. $html .= '

    ' . ($k + 1) . '、' . $v['TABLE_COMMENT'] .' ('. $v['TABLE_NAME']. ')

    '."\n";
  62. $html .= ''."\n";
  63. $html .= '
  64. '."\n";
  65. $html .= '
  66. '."\n";
  67. $html .= '
  68. '."\n";
  69. $html .= '
  70. '."\n";
  71. $html .= '
  72. '."\n";
  73. $html .= '
  74. '."\n";
  75. $html .= '
  76. '."\n";
  77. $html .= '
  78. '."\n";
  79. $html .= '
  80. '."\n";
  81. foreach ($v['COLUMN'] as $f) {
  82. if(!is_array($no_show_field[$v['TABLE_NAME']])){
  83. $no_show_field[$v['TABLE_NAME']] = array();
  84. }
  85. if(!in_array($f['COLUMN_NAME'],$no_show_field[$v['TABLE_NAME']])){
  86. $html .= '
  87. '."\n";
  88. $html .= '
  89. '."\n";
  90. $html .= '
  91. '."\n";
  92. $html .= '
  93. '."\n";
  94. $html .= '
  95. '."\n";
  96. $html .= '
  97. '."\n";
  98. $html .= '
  99. '."\n";
  100. $html .= '
  101. '."\n";
  102. }
  103. }
  104. $html .= '
  105. '."\n";
  106. $html .= '
  107. 字段名数据类型默认值允许非空自动递增备注
    ' . $f['COLUMN_NAME'] . '' . $f['COLUMN_TYPE'] . '' . $f['COLUMN_DEFAULT'] . '' . $f['IS_NULLABLE'] . '' . ($f['EXTRA']=='auto_increment'?'是':' ') . '' . $f['COLUMN_COMMENT'] . '
    '."\n";
  108. }
  109. ?>
复制代码
复制代码


09-08 10:08