PHP實現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法
本文實例講述了PHP實現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法。分享給大家供大家參考,具體如下:
<?php /** * 生成mysql數(shù)據(jù)字典 */ header("Content-type:text/html;charset=utf-8"); // 配置數(shù)據(jù)庫 $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>自動遞增</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>自動生成數(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;">生成時間:' . date('Y-m-d H:i:s') . '</p>'; echo $html; echo '<p style="text-align:left;margin:20px auto;">總共:' . count($tables) . '個數(shù)據(jù)表</p>'; echo '</body></html>'; ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php生成mysql的數(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ù)庫
- PHP實現(xiàn)生成數(shù)據(jù)字典功能示例
相關(guān)文章
php?overtrue/pinyin拓展實現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家介紹了php?overtrue/pinyin拓展實現(xiàn)漢字轉(zhuǎn)拼音示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11PHP連接數(shù)據(jù)庫實現(xiàn)頁面增刪改查效果
這篇文章主要介紹了如何利用PHP實現(xiàn)連接SQL數(shù)據(jù)庫,從而對頁面進(jìn)行增刪改查功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-03-03php 調(diào)用遠(yuǎn)程url的六種方法小結(jié)
php 調(diào)用遠(yuǎn)程url的六種方法,需要的朋友可以參考下。2009-11-11round robin權(quán)重輪循算法php實現(xiàn)代碼
這篇文章主要介紹了round robin權(quán)重輪循算法php實現(xiàn)代碼,需要的朋友可以參考下2016-05-05php class中public,private,protected的區(qū)別以及實例分析
本篇文章是對php class中public,private,protected的區(qū)別以及實例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例
這篇文章主要為大家介紹了php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08php獲取一定范圍內(nèi)取N個不重復(fù)的隨機數(shù)
這篇文章主要介紹了php獲取一定范圍內(nèi)取N個不重復(fù)的隨機數(shù)的方法,通過range函數(shù)創(chuàng)建指定范圍內(nèi)數(shù)組及shuffle進(jìn)行數(shù)組隨機排序,并使用array_slice抽取數(shù)組實現(xiàn)該功能,非常簡單實用,需要的朋友可以參考下2016-05-05