php如何產生mysql資料字典

*文
發布: 2023-03-18 21:44:02
原創
1994 人瀏覽過

把mysql資料庫生成資料字典,直接可用便於查看資料庫表、字段,做一個資料字典是很有必要的。本文主要為大家詳細介紹了php產生mysql的資料字典的相關資料,有興趣的朋友可以參考一下。希望對大家有幫助。

下面的程式碼只需要簡單更改下配置就可以用了,樣式也是挺好的。

<?php 
header(&#39;content-type:text/html;charset=utf-8&#39;); 
define(&#39;DB_HOST&#39;,&#39;localhost&#39;); 
define(&#39;DB_USER&#39;,&#39;root&#39;); 
define(&#39;DB_PASS&#39;,&#39;root&#39;); 
define(&#39;DB_NAME&#39;,&#39;test&#39;); 
define(&#39;DB_PORT&#39;,3306); 
define(&#39;DB_CHAR&#39;,&#39;utf8&#39;); 
define(&#39;APPNAME&#39;,&#39;&#39;); 
$conn=mysql_connect(DB_HOST.&#39;:&#39;.DB_PORT,DB_USER,DB_PASS); 
mysql_select_db(DB_NAME); 
mysql_query(&#39;set names &#39; . DB_CHAR); 
$sql="SHOW TABLE STATUS FROM " . DB_NAME; 
$result=mysql_query($sql); 
$array=array(); 
while($rows=mysql_fetch_assoc($result)){ 
$array[]=$rows; 
} 
// table count 
$tab_count = count($array); 
echo &#39;<!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>&#39;.APPNAME.&#39;--数据字典</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; 
  } 
  .title{font-weight:bold;} 
</style> 
</head> 
<body> 
<p style="text-align:center;background:#D3DCE3;font-size:19px;"> 
  <b>&#39;.APPNAME.&#39;--数据字典</b> 
</p> 
<p style="background:#f3f3f3;text-align:center;">(注:共&#39;.$tab_count.&#39;张表,按ctrl+F查找关键字)</p>&#39;."\n"; 
for($i=0;$i<$tab_count;$i++){ 
echo &#39;<ul type="square">&#39;."\n"; 
echo &#39; <li class="title">&#39;; 
echo ($i+1).&#39;、表名:[&#39; . $array[$i][&#39;Name&#39;] . &#39;]   注释:&#39; . $array[$i][&#39;Comment&#39;]; 
echo &#39;</li>&#39;."\n"; 
//查询数据库字段信息 
$tab_name = $array[$i][&#39;Name&#39;]; 
$sql_tab=&#39;show full fields from `&#39; . $array[$i][&#39;Name&#39;].&#39;`&#39;; 
$tab_result=mysql_query($sql_tab); 
$tab_array=array(); 
  
while($r=mysql_fetch_assoc($tab_result)){ 
  $tab_array[]=$r; 
} 
//show keys 
$keys_result=mysql_query("show keys from `".$array[$i][&#39;Name&#39;].&#39;`&#39;,$conn); 
$arr_keys=mysql_fetch_array($keys_result); 
  echo &#39;<li style="list-style: none outside none;"><table border="0" class="db_table" >&#39;; 
  echo &#39;<tr class="head"> 
    <th style="width:110px">字段</th> 
    <th>类型</th> 
    <th>为空</th> 
    <th>额外</th> 
    <th>默认</th> 
    <th style="width:95px">整理</th> 
    <th>备注</th></tr>&#39;; 
  for($j=0;$j<count($tab_array);$j++){ 
    $key_name=$arr_keys[&#39;Key_name&#39;]; 
    if($key_name="PRIMARY"){ 
      $key_name=&#39;主键(&#39;.$key_name.&#39;)&#39;; 
    } 
    $key_field=$arr_keys[&#39;Column_name&#39;]; 
    if ( $tab_array[$j][&#39;Field&#39;]==$key_field){ 
      $key_value="PK"; 
    }else{ 
      $key_value=""; 
    } 
    echo &#39;    <tr class="&#39;.($j%2==0?"odd":"even").&#39;">&#39;."\n"; 
    echo &#39;     <td>&#39; . $tab_array[$j][&#39;Field&#39;] . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . $tab_array[$j][&#39;Type&#39;] . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . ($key_value!=&#39;&#39;?$key_value:$tab_array[$j][&#39;Null&#39;]) . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . $tab_array[$j][&#39;Extra&#39;] . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . $tab_array[$j][&#39;Default&#39;] . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . $tab_array[$j][&#39;Collation&#39;] . &#39;</td>&#39;."\n"; 
    echo &#39;     <td>&#39; . ($key_value!=&#39;&#39;?$key_name:$tab_array[$j][&#39;Comment&#39;]) . &#39;</td>&#39;."\n"; 
    echo &#39;    </tr>&#39;."\n"; 
  } 
  echo &#39; </table></li>&#39;."\n"; 
  echo &#39;</ul>&#39;."\n"; 
  
} 
echo &#39;</body>&#39;."\n"; 
echo &#39;</html>&#39;."\n";
登入後複製

相關推薦:

#php mysql PDO 查詢操作實例介紹

############################################## ######php mysqli擴展之預處理實例詳解###################php mysql連接專題:最全的php連接mysql資料庫教學總結##### #####

以上是php如何產生mysql資料字典的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!