PHP导出MySQL数据字典

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 2017年11月9日09:30:29 用 PHP mysqli 写的一个类文件, 用来导出MySQL数据字典 导出表信息; 字段信息, 索引信息 可以导出浏览器适用的样式, 也可以导出word文档(默认720px)宽度,字体10px 建议上线前用这个导出一份, 整体过一遍, 防止有些字段, not null, 索引等设置不到位的情况 https://gitee.

2017年11月9日09:30:29

用 PHP mysqli 写的一个类文件, 用来导出MySQL数据字典

导出表信息; 字段信息, 索引信息

可以导出浏览器适用的样式, 也可以导出word文档(默认720px)宽度,字体10px

建议上线前用这个导出一份, 整体过一遍, 防止有些字段, not null, 索引等设置不到位的情况

https://gitee.com/myDcool/PHP-DBDIC

 

用法:

1 include('./DBdic.php');
2 
3 //浏览器显示
4 DBdic::ini('localhost', 'db_name', 'username', 'password')->outForBrowser();
5 
6 //下载word文档
7 DBdic::ini('localhost', 'db_name', 'username', 'password')->outForWord();

 

 

  1 <?php
  2 /**
  3  * 生成mysql数据字典
  4  */
  5 class DBdic
  6 {
  7     public $database = array(); //数据库配置
  8     public $tables = array(); //读取的表信息数组
  9     public $htmlTable = ''; //表格内容
 10     public $html = '';
 11     
 12     public static function ini($host, $dbname, $user, $pwd)
 13     {
 14         return new self($host, $dbname, $user, $pwd);
 15     }
 16     
 17     function __construct($host, $dbname, $user, $pwd)
 18     {
 19         // 配置数据库
 20         $this->database['DB_HOST'] = $host;
 21         $this->database['DB_NAME'] = $dbname;
 22         $this->database['DB_USER'] = $user;
 23         $this->database['DB_PWD'] = $pwd;
 24     
 25         //链接MySQL
 26         $mysqli = mysqli_init();
 27         $mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //超时2s
 28         $mysqli->options(MYSQLI_INIT_COMMAND, "set names utf8mb4;");
 29         $mysqli->real_connect($this->database['DB_HOST'], $this->database['DB_USER'], $this->database['DB_PWD'], $this->database['DB_NAME']) or die("Mysql connect is error.");
 30     
 31         // 取得所有表名
 32         $rs = $mysqli->query('show tables');
 33         $arrTableName = array_column($rs->fetch_all(), $value=0);
 34 
 35         // 取得所有表信息
 36         foreach ($arrTableName as $name) {
 37         
 38             //表注释
 39             $sql = "select * from information_schema.tables where table_schema = '{$this->database['DB_NAME']}' and table_name = '{$name}' "; //查询表信息
 40             $rs = $mysqli->query($sql);
 41             $arrTableInfo = $rs->fetch_assoc();
 42         
 43             //各字段信息
 44             $sql = "select * from information_schema.columns where table_schema ='{$this->database['DB_NAME']}' and table_name = '{$name}' "; //查询字段信息
 45             $rs = $mysqli->query($sql);
 46             $arrColumnInfo = $rs->fetch_all(MYSQLI_ASSOC);
 47         
 48             //索引信息
 49             $sql = "show index from {$name}";
 50             $rs = $mysqli->query($sql);
 51             $arrIndexInfo = $rs->fetch_all(MYSQLI_ASSOC);
 52         
 53             $this->tables[] = array(
 54                 'TABLE' => $arrTableInfo,
 55                 'COLUMN' => $arrColumnInfo,
 56                 'INDEX' => $this->getIndexInfo($arrIndexInfo)
 57             );
 58         }
 59     
 60         //组装HTML
 61         $html = '';
 62         foreach($this->tables as $v)
 63         {
 64             $html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
 65             $html .= '<caption>' . $v['TABLE']['TABLE_NAME'] . ' ' . $v['TABLE']['TABLE_COMMENT'] . '</caption>';
 66             $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>索引/自增</th><th>备注(字段数: '. count($v['COLUMN']).')</th></tr>';
 67         
 68             foreach ($v['COLUMN'] AS $f) {
 69                 $html .= '<tr>';
 70                 $html .= '<td class="c1">' . $f['COLUMN_NAME']      . '</td>';
 71                 $html .= '<td class="c2">' . $f['COLUMN_TYPE']      . '</td>';
 72                 $html .= '<td class="c3">' . $f['COLUMN_DEFAULT']   . '</td>';
 73                 $html .= '<td class="c4">' . $f['IS_NULLABLE']      . '</td>';
 74                 $html .= '<td class="c5">' . $f['COLUMN_KEY'].' '.$f['EXTRA']. '</td>';
 75                 $html .= '<td class="c6">' . $f['COLUMN_COMMENT']   . '</td>';
 76                 $html .= '</tr>';
 77             }
 78             $html .= '</tbody></table>';
 79         
 80             $html .= '<table style="border-top:hidden"  cellspacing="0" cellpadding="0" align="center">';
 81             $html .= '<tr><th>索引名</th><th>索引顺序</th></tr>';
 82             foreach ($v['INDEX'] as $indexName => $indexContent) {
 83                 $html .= '<tr>';
 84                 $html .= '<td class="c7">' . $indexName . '</td>';
 85                 $html .= '<td>' . implode('; ', $indexContent) . '</td>';
 86                 $html .= '</tr>';
 87             }
 88             $html .= '</table><br>';
 89         }
 90         $this->htmlTable = $html;
 91     }
 92     
 93     //整合单个表的所有索引(将复合索引归纳到一起)
 94     function getIndexInfo($arrIndexInfo)
 95     {
 96         $index = array();
 97         foreach ($arrIndexInfo as $v) {
 98             $unique = ($v['Non_unique'] == 0) ? '(unique)' : '';
 99             $index[$v['Key_name']][] = $v['Seq_in_index'].': '.$v['Column_name'].$unique;
100         }
101         
102         return $index;
103     }
104     
105     //输出到浏览器, 表格宽度用百分比
106     function outForBrowser()
107     {
108         header("Content-type:text/html;charset=utf-8");
109         $html = '<html>
110               <meta charset="utf-8">
111               <title>自动生成数据字典</title>
112               <style>
113                 body,td,th {font-family:"宋体"; font-size:14px;}
114                 table,h1,p{width:80%;margin:0px auto;}
115                 table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
116                 table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
117                 table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:14px; border:1px solid #CCC;padding-left:5px;}
118                 table td{height:20px; font-size:14px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}
119                 .c1{ width: 10%;}
120                 .c2{ width: 10%;}
121                 .c3{ width: 5%;}
122                 .c4{ width: 5%;}
123                 .c5{ width: 10%;}
124                 .c6{ width: 60%;}
125                 .c7{ width: 10%;}
126               </style>
127               <body>';
128         $html .= '<h1 style="text-align:center;">'.$this->database['DB_NAME'].'数据字典</h1>';
129         $html .= '<p style="text-align:center;margin:20px auto;">生成时间:' . date('Y-m-d H:i:s') . '  总共:' . count($this->tables) . '个数据表</p>';
130         $html .= $this->htmlTable;
131         $html .= '</body></html>';
132         
133         $this->html = $html;
134         echo $html;
135         // return $this;
136     }
137     
138     //输出到word文档, 固定宽度为720px
139     function outForWord()
140     {
141         /* 生成word */
142         header("Content-type:text/html;charset=utf-8");
143         header( "Content-type:application/vnd.ms-word" );
144         header( "Content-Disposition:attachment;filename={$this->database['DB_NAME']}数据字典.doc" );
145     
146         $html = '<html>
147               <meta charset="utf-8">
148               <title>自动生成数据字典</title>
149               <style>
150                 body,td,th {font-family:"宋体"; font-size:14px;}
151                 table,h1,p{width:720px;margin:0px auto;}
152                 table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
153                 table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
154                 table th{text-align:left; font-weight:bold;height:20px; line-height:20px; font-size:11px; border:1px solid #CCC;padding-left:5px;}
155                 table td{height:20px; font-size:11px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}
156                 .c1{ width: 100px;}
157                 .c2{ width: 110px;}
158                 .c3{ width: 50px;}
159                 .c4{ width: 55px;}
160                 .c5{ width: 100px;}
161                 .c6{ width: 300px;}
162                 .c7{ width: 200px;}
163               </style>
164               <body>';
165         $html .= '<h1 style="text-align:center;">'.$this->database['DB_NAME'].'数据字典</h1>';
166         $html .= '<p style="text-align:center;margin:20px auto;">生成时间:' . date('Y-m-d H:i:s') . '  总共:' . count($this->tables) . '个数据表</p>';
167         $html .= $this->htmlTable;
168         $html .= '</body></html>';
169         
170         $this->html = $html;
171         echo $html;
172         // return $this;
173     }
174     
175     function out()
176     {
177         // echo $this->html;
178     }
179     
180 }
View Code

 

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
13天前
|
安全 关系型数据库 MySQL
PHP与MySQL交互:从入门到实践
【9月更文挑战第20天】在数字时代的浪潮中,掌握PHP与MySQL的互动成为了开发动态网站和应用程序的关键。本文将通过简明的语言和实例,引导你理解PHP如何与MySQL数据库进行对话,开启你的编程之旅。我们将从连接数据库开始,逐步深入到执行查询、处理结果,以及应对常见的挑战。无论你是初学者还是希望提升技能的开发者,这篇文章都将为你提供实用的知识和技巧。让我们一起探索PHP与MySQL交互的世界,解锁数据的力量!
|
8天前
|
关系型数据库 MySQL 数据库
ORM对mysql数据库中数据进行操作报错解决
ORM对mysql数据库中数据进行操作报错解决
32 2
|
8天前
|
SQL 关系型数据库 MySQL
MySQL如何排查和删除重复数据
该文章介绍了在MySQL中如何排查和删除重复数据的方法,包括通过组合字段生成唯一标识符以及使用子查询和聚合函数来定位并删除重复记录的具体步骤。
26 2
|
3天前
|
消息中间件 canal 关系型数据库
Maxwell:binlog 解析器,轻松同步 MySQL 数据
Maxwell:binlog 解析器,轻松同步 MySQL 数据
30 11
|
2天前
|
关系型数据库 MySQL 数据库
MySQL的语法涵盖了数据定义、数据操作、数据查询和数据控制等多个方面
MySQL的语法涵盖了数据定义、数据操作、数据查询和数据控制等多个方面
13 5
|
9天前
|
关系型数据库 MySQL 数据库
Python MySQL查询返回字典类型数据的方法
通过使用 `mysql-connector-python`库并选择 `MySQLCursorDict`作为游标类型,您可以轻松地将MySQL查询结果以字典类型返回。这种方式提高了代码的可读性,使得数据操作更加直观和方便。上述步骤和示例代码展示了如何实现这一功能,希望对您的项目开发有所帮助。
26 4
|
17天前
|
存储 关系型数据库 MySQL
技术解析:MySQL中取最新一条重复数据的方法
以上提供的两种方法都可以有效地从MySQL数据库中提取每个类别最新的重复数据。选择哪种方法取决于具体的使用场景和MySQL版本。子查询加分组的方法兼容性更好,适用于所有版本的MySQL;而窗口函数方法代码更简洁,执行效率可能更高,但需要MySQL 8.0及以上版本。在实际应用中,应根据数据量大小、查询性能需求以及MySQL版本等因素综合考虑,选择最合适的实现方案。
86 6
|
17天前
|
关系型数据库 MySQL 数据处理
针对MySQL亿级数据的高效插入策略与性能优化技巧
在处理MySQL亿级数据的高效插入和性能优化时,以上提到的策略和技巧可以显著提升数据处理速度,减少系统负担,并保持数据的稳定性和一致性。正确实施这些策略需要深入理解MySQL的工作原理和业务需求,以便做出最适合的配置调整。
69 6
|
6天前
|
存储 SQL 关系型数据库
mysql删除 所有数据
mysql删除 所有数据
|
16天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
下一篇
无影云桌面