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

說了這么多廢話了,終于開始寫代碼了:
<?php
/**
* 生成mysql數(shù)據(jù)字典
*/
//配置數(shù)據(jù)庫
$dbserver = "127.0.0.1";
$dbusername = "賬號(hào)";
$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>默認(rèn)值</th>
<th>允許非空</th>
<th>自動(dòng)遞增</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è)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php生成mysql的數(shù)據(jù)字典
- PHP實(shí)現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法
- ThinkPHP框架實(shí)現(xiàn)的MySQL數(shù)據(jù)庫備份功能示例
- PHP備份/還原MySQL數(shù)據(jù)庫的代碼
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫備份類
- 使用PHP備份MYSQL數(shù)據(jù)的多種方法
- php MYSQL 數(shù)據(jù)備份類
- php實(shí)現(xiàn)MySQL數(shù)據(jù)庫備份與還原類實(shí)例
- 使用php自動(dòng)備份數(shù)據(jù)庫表的實(shí)現(xiàn)方法
- 用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫
相關(guān)文章
phpMyAdmin出現(xiàn)無法載入 mcrypt 擴(kuò)展,請(qǐng)檢查PHP配置的解決方法
出現(xiàn)以下幾種情況后可能會(huì)造成運(yùn)行phpmyadmin程序提示 無法載入 mcrypt 擴(kuò)展,請(qǐng)檢查 PHP 配置 的 錯(cuò)誤提示2012-03-03
在php7中MongoDB實(shí)現(xiàn)模糊查詢的方法詳解
MongoDB模糊查詢語句相信對(duì)大家來說都不陌生,這篇文章主要給大家介紹了在php 7中MongoDB實(shí)現(xiàn)模糊查詢的方法,文中給出了詳細(xì)的介紹和示例代碼,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友一起來看看吧。2017-05-05
PHP中的自動(dòng)加載操作實(shí)現(xiàn)方法詳解
這篇文章主要介紹了PHP中的自動(dòng)加載操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了php自動(dòng)加載的概念、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
PHP表單遞交控件名稱含有點(diǎn)號(hào)(.)會(huì)被轉(zhuǎn)化為下劃線(_)的處理方法
做項(xiàng)目的過程中發(fā)現(xiàn),表單遞交就是不成功,后來發(fā)現(xiàn)原來我給控件的名字不規(guī)范導(dǎo)致,控件遞交到后端之后,發(fā)現(xiàn)所有我控件名字中含有.號(hào)的名字,遞交過之后都會(huì)被轉(zhuǎn)化成下劃線_2013-01-01
解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
本篇文章是對(duì)mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

