PHP實現(xiàn)生成數(shù)據(jù)字典功能示例
本文實例講述了PHP實現(xiàn)生成數(shù)據(jù)字典功能。分享給大家供大家參考,具體如下:
最近時間緊迫,沒有時間發(fā)博客,趁現(xiàn)在有點時間向大家分享一個知識點。在咱們做開發(fā)的時候 ,也許經(jīng)常會遇到對數(shù)據(jù)庫分析,做一個數(shù)據(jù)字典,那么現(xiàn)在好處來了,大家只需要關(guān)注我所發(fā)送的這個鏈接輕輕松松幫你們搞定數(shù)據(jù)字典的設(shè)計,再也不用你們下載數(shù)據(jù)字典那個工具,作為一個開發(fā)人員,隨便幾行代碼就應(yīng)該會搞定這個知識,那么現(xiàn)在廢話我也不多說了,大家看底下的代碼,大家只需要改一下你的數(shù)據(jù)庫賬號和密碼然后運行一下就可以了。
效果展示:

說了這么多廢話了,終于開始寫代碼了:
<?php
/**
* 生成mysql數(shù)據(jù)字典
*/
//配置數(shù)據(jù)庫
$dbserver = "127.0.0.1";
$dbusername = "賬號";
$dbpassword = "密碼";
$database = "數(shù)據(jù)庫";
//其他配置
$title = '數(shù)據(jù)字典';
$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];
}
//循環(huán)取得所有表的備注及表中列消息
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 = '';
//循環(huán)所有表
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>數(shù)據(jù)類型</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:#efefef;}
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:26px; font-size:12px; border:1px solid #CCC;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
.c1{ width: 120px;}
.c2{ width: 120px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 270px;}
</style>
</head>
<body>';
echo '<h1 style="text-align:center;">'.$title.'</h1>';
echo $html;
echo '</body></html>';
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php生成mysql的數(shù)據(jù)字典
- PHP實現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法
- ThinkPHP框架實現(xiàn)的MySQL數(shù)據(jù)庫備份功能示例
- PHP備份/還原MySQL數(shù)據(jù)庫的代碼
- php實現(xiàn)mysql數(shù)據(jù)庫備份類
- 使用PHP備份MYSQL數(shù)據(jù)的多種方法
- php MYSQL 數(shù)據(jù)備份類
- php實現(xiàn)MySQL數(shù)據(jù)庫備份與還原類實例
- 使用php自動備份數(shù)據(jù)庫表的實現(xiàn)方法
- 用PHP實現(xiàn)XML備份Mysql數(shù)據(jù)庫
相關(guān)文章
php自動識別文件編碼并轉(zhuǎn)換為UTF-8的方法
PHP轉(zhuǎn)換文件編碼是一個比較簡單的事情,但是難的是傳遞中文參數(shù)的時候,有時候不知道是什么編碼,結(jié)果造成了亂碼的現(xiàn)象。這篇文章主要介紹了php自動識別編碼并轉(zhuǎn)換為UTF-8的方法,需要的朋友可以參考下2014-06-06
解析PHP函數(shù)array_flip()在重復(fù)數(shù)組元素刪除中的作用
本篇文章是對PHP函數(shù)array_flip()在重復(fù)數(shù)組元素刪除中的作用進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP5.5在windows安裝使用memcached服務(wù)端的方法
這篇文章主要介紹了PHP5.5在windows安裝使用memcached服務(wù)端的方法,需要的朋友可以參考下2014-04-04
解析阿里云ubuntu12.04環(huán)境下配置Apache+PHP+PHPmyadmin+MYsql
本篇文章是對在阿里云ubuntu12.04環(huán)境下配置Apache+PHP+PHPmyadmin+MYsql的具體操作流程進行了詳細的分析介紹,需要的朋友參考下2013-06-06

