由于项目开发用了比较多的表 ,为了快速获取数据字典,通过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'] . '&nbsp;</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">&nbsp;' . $f ['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4">&nbsp;' . $f ['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f ['EXTRA'] == 'auto_increment' ? '是' : '&nbsp;') . '</td>';
$html .= '<td class="c6">&nbsp;' . $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数据字典代码的更多相关文章

  1. 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典

    下面提到的软件大家可以在下面的链接下载. 大家可以参考下面的操作录制视频来完成相关的操作. 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典.wmv_免费高速下 ...

  2. php生成mysql数据字典

    <?php /** * 生成mysql数据字典 */ // 配置数据库 $database = array(); $database['DB_HOST'] = '127.0.0.1'; $dat ...

  3. php 生成mysql数据字典 (php5.5-5.6)

    <?php /** * 生成mysql数据字典 */ //配置数据库 $dbserver = "127.0.0.1"; $dbusername = "root&qu ...

  4. 生成mysql数据字典

    data_dictionary.php <?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf- ...

  5. [功能集锦] 003 - 一键生成mysql数据字典/数据库速查表

    写在前面: 因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失.故写此篇,以便复用,也希望对大家有点帮助. 随笔内容不高级,如有不妥,不吝指正. ps:有另一篇详细随笔可以参 ...

  6. 使用Navicat快速生成MySQL数据字典

    1.通过information_schema.COLUMNS表 查询该表可得到所需字段信息 SELECT * FROM information_schema.COLUMNS; 如下图所示: 2.示例 ...

  7. 生成MySql数据库的数据字典代码参考

    Code: /** * 生成mysql数据字典 */ //配置数据库 $dbserver = "127.0.0.1"; $dbusername = "root" ...

  8. php生成数据字典,代码

    <?php /** * 生成mysql数据字典 */ header("Content-type:text/html;charset=utf-8"); // 配置数据库 $da ...

  9. PHP MYSQL数据字典

    <?php /** * 生成mysql数据字典 */ header ( "Content-type: text/html; charset=utf-8" ); // 配置数据 ...

随机推荐

  1. Linq知识大全

    select的源码public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable< ...

  2. yum使用详细

    1.使用YUM查找软件包 命令:yum search~ 2.列出所有可安装的软件包 命令:yum list 3.列出所有可更新的软件包 命令:yum list updates 4.列出所有已安装的软件 ...

  3. alibaba的COBAR真是强大.

    近好不容易抽空研究了下Cobar,感觉这个产品确实很不错(在文档方面比Amoeba强多了),特此推荐给大家.Cobar是阿里巴巴研发的关系型数据 的分布式处理系统,该产品成功替代了原先基于Oracle ...

  4. 有关sybase的一些零星经验

    clear transaction log >dump transaction master with truncate_only >dump transaction master wit ...

  5. Custom template tags and filters

    Code layout Custom template tags and filters must live inside a Django app. If they relate to an exi ...

  6. 缓存 Cache

    Controllers层 public class HomeController : Controller    {        //        // GET: /Home/       // ...

  7. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  8. 用 OUTLOOK VBA 生成 自定义文件夹 邮件列表

    Option Explicit Sub TestFolder() 'Dim outlookapp, myitem, myfolder 'Dim mailcounts As Integer ' ' 'S ...

  9. 使用ImageMagick和Tesseract进行简单数字图像识别

    使用ImageMagick和Tesseract进行简单数字图像识别 由于直接使用 tesseract 进行识别,识别率很低, ImageMagick 安装.配置及使用: 平台:winXP 1. 安装I ...

  10. uva 10881 - Piotr's Ants

    这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...