欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php生成mysql的数据字典

程序员文章站 2024-02-23 14:41:22
把mysql数据库生成数据字典,直接可用便于查看数据库表、字段,做一个数据字典是很有必要的,下面只需要简单更改下配置就可以用了,样式也是挺好的。

把mysql数据库生成数据字典,直接可用便于查看数据库表、字段,做一个数据字典是很有必要的,下面只需要简单更改下配置就可以用了,样式也是挺好的。

<?php 
header('content-type:text/html;charset=utf-8'); 
define('db_host','localhost'); 
define('db_user','root'); 
define('db_pass','root'); 
define('db_name','test'); 
define('db_port',3306); 
define('db_char','utf8'); 
define('appname',''); 
$conn=mysql_connect(db_host.':'.db_port,db_user,db_pass); 
mysql_select_db(db_name); 
mysql_query('set names ' . 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 '<!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; 
  } 
  .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'].'`'; 
$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]['name'].'`',$conn); 
$arr_keys=mysql_fetch_array($keys_result); 
  echo '<li style="list-style: none outside none;"><table border="0" class="db_table" >'; 
  echo '<tr class="head"> 
    <th style="width:110px">字段</th> 
    <th>类型</th> 
    <th>为空</th> 
    <th>额外</th> 
    <th>默认</th> 
    <th style="width:95px">整理</th> 
    <th>备注</th></tr>'; 
  for($j=0;$j<count($tab_array);$j++){ 
    $key_name=$arr_keys['key_name']; 
    if($key_name="primary"){ 
      $key_name='主键('.$key_name.')'; 
    } 
    $key_field=$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"; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。