当前位置:Gxlcms > PHP教程 > 数据库字典生成

数据库字典生成

时间:2021-07-01 10:21:17 帮助过:2人阅读

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

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

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

人气教程排行