php 生成mysql数据字典代码
由于项目开发用了比较多的表 ,为了快速获取数据字典,通过php代码的方式来获取表结构和表注释。代码如下:
<?php
/**
* 生成mysql数据字典
*/
header ( "Content-type: text/html; charset=utf-8" ); // 配置数据库
$dbserver = "localhost";
$dbusername = "数据库用户名";
$dbpassword = "数据库密码";
$database = "数据库名称"; // 其他配置
$title = '数据字典'; $mysql_conn = @mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." );
mysql_select_db ( $database, $mysql_conn );
mysql_query ( 'SET NAMES utf8', $mysql_conn );
$table_result = mysql_query ( 'show tables', $mysql_conn );
// 取得所有的表名
while ( $row = mysql_fetch_array ( $table_result ) ) {
$tables [] ['TABLE_NAME'] = $row [0];
} // 循环取得所有表的备注及表中列消息
foreach ( $tables as $k => $v ) {
$sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.TABLES ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
$table_result = mysql_query ( $sql, $mysql_conn );
while ( $t = mysql_fetch_array ( $table_result ) ) {
$tables [$k] ['TABLE_COMMENT'] = $t ['TABLE_COMMENT'];
} $sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.COLUMNS ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'"; $fields = array ();
$field_result = mysql_query ( $sql, $mysql_conn );
while ( $t = mysql_fetch_array ( $field_result ) ) {
$fields [] = $t;
}
$tables [$k] ['COLUMN'] = $fields;
}
mysql_close ( $mysql_conn ); $html = '';
// 循环所有表
foreach ( $tables as $k => $v ) {
// $html .= '<p><h2>'. $v['TABLE_COMMENT'] . ' </h2>';
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption>' . $v ['TABLE_NAME'] . ' ' . $v ['TABLE_COMMENT'] . '</caption>';
$html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
<th>允许非空</th>
<th>自动递增</th><th>备注</th></tr>';
$html .= ''; foreach ( $v ['COLUMN'] as $f ) {
$html .= '<tr><td class="c1">' . $f ['COLUMN_NAME'] . '</td>';
$html .= '<td class="c2">' . $f ['COLUMN_TYPE'] . '</td>';
$html .= '<td class="c3"> ' . $f ['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4"> ' . $f ['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f ['EXTRA'] == 'auto_increment' ? '是' : ' ') . '</td>';
$html .= '<td class="c6"> ' . $f ['COLUMN_COMMENT'] . '</td>';
$html .= '</tr>';
}
$html .= '</tbody></table></p>';
} // 输出
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>' . $title . '</title>
<style>
body,td,th {font-family:"宋体"; font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#6089D4;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:25px; font-size:16px; border:3px solid #fff; color:#ffffff; padding:5px;}
table td{height:25px; font-size:12px; border:3px solid #fff; background-color:#f0f0f0; padding:5px;}
.c1{ width: 150px;}
.c2{ width: 130px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 300px;}
</style>
</head>
<body>';
echo '<h1 style="text-align:center;">' . $title . '</h1>';
echo $html;
echo '</body></html>'; ?>
php 生成mysql数据字典代码的更多相关文章
- 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典
下面提到的软件大家可以在下面的链接下载. 大家可以参考下面的操作录制视频来完成相关的操作. 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典.wmv_免费高速下 ...
- php生成mysql数据字典
<?php /** * 生成mysql数据字典 */ // 配置数据库 $database = array(); $database['DB_HOST'] = '127.0.0.1'; $dat ...
- php 生成mysql数据字典 (php5.5-5.6)
<?php /** * 生成mysql数据字典 */ //配置数据库 $dbserver = "127.0.0.1"; $dbusername = "root&qu ...
- 生成mysql数据字典
data_dictionary.php <?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf- ...
- [功能集锦] 003 - 一键生成mysql数据字典/数据库速查表
写在前面: 因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失.故写此篇,以便复用,也希望对大家有点帮助. 随笔内容不高级,如有不妥,不吝指正. ps:有另一篇详细随笔可以参 ...
- 使用Navicat快速生成MySQL数据字典
1.通过information_schema.COLUMNS表 查询该表可得到所需字段信息 SELECT * FROM information_schema.COLUMNS; 如下图所示: 2.示例 ...
- 生成MySql数据库的数据字典代码参考
Code: /** * 生成mysql数据字典 */ //配置数据库 $dbserver = "127.0.0.1"; $dbusername = "root" ...
- php生成数据字典,代码
<?php /** * 生成mysql数据字典 */ header("Content-type:text/html;charset=utf-8"); // 配置数据库 $da ...
- PHP MYSQL数据字典
<?php /** * 生成mysql数据字典 */ header ( "Content-type: text/html; charset=utf-8" ); // 配置数据 ...
随机推荐
- 【python】python的二元表达式和三元表达式
二元表达式 x,y=4,3if x>y: s = yelse: s= x print s x if x<y else y 三元表达式: >>> def f(x,y): ...
- PYTHON 获取机器硬件信息及状态
#!/usr/bin/env python # encoding: utf-8 from optparse import OptionParser import os import re import ...
- SortedList的用法
1.SortedList定义 System.Collections.SortedList类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问.SortedList 在内部维护两个数组以存储列表 ...
- android 布局权重问题(转载)
//权重和父容器orientation有关 horizontal 指水平方向权重 android:layout_width vertical 指垂直方向权重 android:layout_he ...
- Matlab中Rand()函数用法
一.理论准备 matlab函数randn:产生均值为0,方差 σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数. 用法:Y = randn(n),返回一个n*n的随机项的矩阵.如果n不是 ...
- BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
Description FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:).在赛场上,奶牛们按1..N依次编号.每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平 ...
- The state of Web Components
Web Components have been on developers’ radars for quite some time now. They were first introduced b ...
- 【POJ 1639】 Picnic Planning (最小k度限制生成树)
[题意] 有n个巨人要去Park聚会.巨人A和先到巨人B那里去,然后和巨人B一起去Park.B君是个土豪,他家的停车场很大,可以停很多车,但是Park的停车场是比较小.只能停k辆车.现在问你在这个限制 ...
- vlc
源码下载地址:http://download.videolan.org/pub/videolan/vlc/ 编译依赖: sudo apt-get install liblua5.2-dev sudo ...
- 不重复查询mysql
select EquipmentSID,MIN(MatureTime),MIN(ISlock) from table group by name String sql =” Select * from ...