1. 程式人生 > 其它 >[PHP] 生成資料庫字典的PHP程式碼支援PDO擴充套件

[PHP] 生成資料庫字典的PHP程式碼支援PDO擴充套件

有時候沒有資料庫字典設計文件,根據現有的資料庫註釋進行生成一份

<?php
header('content-type:text/html;charset=utf-8');
define('DB_HOST','192.168.11.7');
define('DB_USER','root');
define('DB_PASS','centos');
define('DB_NAME','new_admin');
define('DB_PORT',3306);
define('DB_CHAR','utf8');
define('APPNAME','new_admin');
$dbh = new PDO("
mysql:host=".DB_HOST.":".DB_PORT.";dbname=".DB_NAME,DB_USER,DB_PASS); $dbh->exec('set names ' . DB_CHAR); $sth = $dbh->prepare("SHOW TABLE STATUS FROM " . DB_NAME); $sth->execute(); $array = $sth->fetchAll(PDO::FETCH_ASSOC); $tab_count = count($array); echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="zh"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>'.APPNAME.'--資料字典</title> <style type="text/css"> table caption, table th, table td { padding:
0.1em 0.5em 0.1em 0.5em; margin: 0.1em; vertical-align: top; } th { font-weight: bold; color: black; background: #D3DCE3; } table tr.odd th, .odd { background: #E5E5E5; } table tr.even th, .even { background: #f3f3f3; } .db_table{ border-top:1px solid #333; width: 100%; } .title{font-weight:bold;} </style> </head> <body> <div style="text-align:center;background:#D3DCE3;font-size:19px;"> <b>'.APPNAME.'--資料字典</b> </div> <div style="background:#f3f3f3;text-align:center;">(注:共'.$tab_count.'張表,按ctrl+F查詢關鍵字)</div>'."\n"; for($i=0;$i<$tab_count;$i++){ echo '<ul type="square">'."\n"; echo ' <li class="title">'; echo ($i+1).'、表名:[' . $array[$i]['Name'] . '] 註釋:' . $array[$i]['Comment']; echo '</li>'."\n"; //查詢資料庫欄位資訊 $tab_name = $array[$i]['Name']; $sql_tab='show full fields from `' . $array[$i]['Name'].'`'; $sth = $dbh->prepare($sql_tab); $sth->execute(); $tab_array = $sth->fetchAll(PDO::FETCH_ASSOC); //show keys $sth = $dbh->prepare("show keys from `".$array[$i]['Name'].'`'); $sth->execute(); $arr_keys = $sth->fetchAll(PDO::FETCH_ASSOC); echo '<li style="list-style: none outside none;"><table border="0" class="db_table" >'; echo '<tr class="head"> <th style="width:180px">欄位</th> <th style="width:150px">型別</th> <th style="width:50px">為空</th> <th style="width:180px">額外</th> <th style="width:180px">預設</th> <th style="width:150px">整理</th> <th>備註</th></tr>'; for($j=0;$j<count($tab_array);$j++){ $key_name=isset($arr_keys['Key_name'])? $arr_keys['Key_name']:""; if($key_name="PRIMARY"){ $key_name='主鍵('.$key_name.''; } $key_field=isset($arr_keys['Column_name']) ? $arr_keys['Column_name']:""; if ( $tab_array[$j]['Field']==$key_field){ $key_value="PK"; }else{ $key_value=""; } echo ' <tr class="'.($j%2==0?"odd":"even").'">'."\n"; echo ' <td>' . $tab_array[$j]['Field'] . '</td>'."\n"; echo ' <td>' . $tab_array[$j]['Type'] . '</td>'."\n"; echo ' <td>' . ($key_value!=''?$key_value:$tab_array[$j]['Null']) . '</td>'."\n"; echo ' <td>' . $tab_array[$j]['Extra'] . '</td>'."\n"; echo ' <td>' . $tab_array[$j]['Default'] . '</td>'."\n"; echo ' <td>' . $tab_array[$j]['Collation'] . '</td>'."\n"; echo ' <td>' . ($key_value!=''?$key_name:$tab_array[$j]['Comment']) . '</td>'."\n"; echo ' </tr>'."\n"; } echo ' </table></li>'."\n"; echo '</ul>'."\n"; } echo '</body>'."\n"; echo '</html>'."\n";

效果是這樣的