PHP實(shí)現(xiàn)獲取并生成數(shù)據(jù)庫(kù)字典的方法
本文實(shí)例講述了PHP實(shí)現(xiàn)獲取并生成數(shù)據(jù)庫(kù)字典的方法。分享給大家供大家參考,具體如下:

<?php
/**
* 生成mysql數(shù)據(jù)字典
*/
header("Content-type:text/html;charset=utf-8");
// 配置數(shù)據(jù)庫(kù)
$database = array();
$database['DB_HOST'] = 'localhost';
$database['DB_NAME'] = 'test';
$database['DB_USER'] = 'root';
$database['DB_PWD'] = '';
$mysql_conn = @mysql_connect("{$database['DB_HOST']}", "{$database['DB_USER']}", "{$database['DB_PWD']}") or die("Mysql connect is error.");
mysql_select_db($database['DB_NAME'], $mysql_conn);
$result = mysql_query('show tables', $mysql_conn);
mysql_query("set names utf8");
// 取得所有表名
while ($row = mysql_fetch_array($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['DB_NAME']}'";
$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['DB_NAME']}'";
$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)所有表
//print_r($tables);
foreach($tables as $k => $v)
{
$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 .= '<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>';
}
/* 生成word */
//header ( "Content-type:application/vnd.ms-word" );
//header ( "Content-Disposition:attachment;filename={$database['DB_NAME']}數(shù)據(jù)字典.doc" );
/* 生成excel*/
//header ( "Content-type:application/vnd.ms-excel" );
//header ( "Content-Disposition:attachment;filename={$database['DB_NAME']}數(shù)據(jù)字典.xls" );
// 輸出
echo '<html>
<meta charset="utf-8">
<title>自動(dòng)生成數(shù)據(jù)字典</title>
<style>
body,td,th {font-family:"宋體"; font-size:12px;}
table,h1,p{width:960px;margin:0px auto;}
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;padding-left:5px;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}
.c1{ width: 150px;}
.c2{ width: 150px;}
.c3{ width: 80px;}
.c4{ width: 100px;}
.c5{ width: 100px;}
.c6{ width: 300px;}
</style>
<body>';
echo '<h1 style="text-align:center;">'.$database['DB_NAME'].'數(shù)據(jù)字典</h1>';
echo '<p style="text-align:center;margin:20px auto;">生成時(shí)間:' . date('Y-m-d H:i:s') . '</p>';
echo $html;
echo '<p style="text-align:left;margin:20px auto;">總共:' . count($tables) . '個(gè)數(shù)據(jù)表</p>';
echo '</body></html>';
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php生成mysql的數(shù)據(jù)字典
- ThinkPHP框架實(shí)現(xiàn)的MySQL數(shù)據(jù)庫(kù)備份功能示例
- PHP備份/還原MySQL數(shù)據(jù)庫(kù)的代碼
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)備份類
- 使用PHP備份MYSQL數(shù)據(jù)的多種方法
- php MYSQL 數(shù)據(jù)備份類
- php實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)備份與還原類實(shí)例
- 使用php自動(dòng)備份數(shù)據(jù)庫(kù)表的實(shí)現(xiàn)方法
- 用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫(kù)
- PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
相關(guān)文章
php?overtrue/pinyin拓展實(shí)現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家介紹了php?overtrue/pinyin拓展實(shí)現(xiàn)漢字轉(zhuǎn)拼音示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
php查看一個(gè)變量的占用內(nèi)存的實(shí)例代碼
在本篇文章里小編給各位分享的是關(guān)于php查看一個(gè)變量的占用內(nèi)存的實(shí)例代碼,需要的朋友們可以學(xué)習(xí)下。2020-03-03
PHP實(shí)現(xiàn)對(duì)數(shù)組分頁(yè)處理實(shí)例詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)對(duì)數(shù)組分頁(yè)處理,結(jié)合實(shí)例形式分析了php封裝的數(shù)組分頁(yè)類定義與使用技巧,需要的朋友可以參考下2017-02-02
PHP連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)頁(yè)面增刪改查效果
這篇文章主要介紹了如何利用PHP實(shí)現(xiàn)連接SQL數(shù)據(jù)庫(kù),從而對(duì)頁(yè)面進(jìn)行增刪改查功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-03-03
php 調(diào)用遠(yuǎn)程url的六種方法小結(jié)
php 調(diào)用遠(yuǎn)程url的六種方法,需要的朋友可以參考下。2009-11-11
round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼
這篇文章主要介紹了round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-05-05
php class中public,private,protected的區(qū)別以及實(shí)例分析
本篇文章是對(duì)php class中public,private,protected的區(qū)別以及實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php編寫的mysqli增刪改查數(shù)據(jù)庫(kù)操作類示例
這篇文章主要為大家介紹了php編寫的mysqli增刪改查數(shù)據(jù)庫(kù)操作類示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
php獲取一定范圍內(nèi)取N個(gè)不重復(fù)的隨機(jī)數(shù)
這篇文章主要介紹了php獲取一定范圍內(nèi)取N個(gè)不重復(fù)的隨機(jī)數(shù)的方法,通過(guò)range函數(shù)創(chuàng)建指定范圍內(nèi)數(shù)組及shuffle進(jìn)行數(shù)組隨機(jī)排序,并使用array_slice抽取數(shù)組實(shí)現(xiàn)該功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-05-05

