读取表结构,生成表格文档

<?php 

header("content-type=text/html;charset=utf-8");

$link = mysql_connect('localhost', 'root', '');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db("qujiang",$link) or die ('Can\'t use foo : ' . mysql_error());

mysql_query("set names 'utf8'",$link); 

$info = mysql_query("SELECT table_name tables,TABLE_COMMENT comment FROM INFORMATION_SCHEMA.TABLES  WHERE table_schema = 'qujiang'",$link) or die("Invalid query: " . mysql_error());

$i = 0;

while($arr = mysql_fetch_assoc($info)){

$data[$i] = array('tables'=>$arr['tables'],'comment'=>$arr['comment']);

$field_data = array();

$dql = 'show full fields from qujiang.`'.$arr['tables'].'`';

$field_info = mysql_query($dql);

while($fieldarr = mysql_fetch_assoc($field_info)){

$field_data[] = $fieldarr;

}

$comment = $arr['comment'] ? '('.$arr['comment'].')' : '';

$data[$i]['table'] = $arr['tables'].$comment;

$data[$i]['fields'] = $field_data;

$i++;

}//print_r($data);

mysql_close($link);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<style type="text/css">  

        table {  

            border: 1px solid #B1CDE3;  

            padding:0;   

            margin:0 auto;  

            border-collapse: collapse;  

        }  

          

        td {  

            border: 1px solid #B1CDE3;  

            background: #fff;  

            font-size:12px;  

            padding: 3px 3px 3px 8px;  

            color: #4f6b72;  

        }  

        .s{

background: #def;

        font-size:13px;

        }

</style> 

</head>

<body>

<?php foreach($data as $keys=>$vo){ ?>

<div style="height: 40px; line-height: 40px;"><?php echo $vo['table'];?></div>


<table width="100%" align="center">

<tr style="text-align: center; COLOR: #0076C8; BACKGROUND-COLOR: #CCC; height:35px; font-weight: bold">

<td class="s">字段名</td>

<td class="s">类型</td>

<td class="s">Null</td>

<td class="s">Key</td>

<td class="s">Default</td>

<td class="s">Extra</td>

<td class="s">描述</td>

</tr>

<?php foreach($vo['fields'] as $k=>$vf){ ?>

<tr>

<td nowrap="nowrap" align="left"><?php echo $vf['Field'];?></td>

<td align="left"><font size="2"><?php echo $vf['Type'];?></font></td>

<td align="left"><font size="2"><?php echo $vf['Null'];?></font></td>

<td align="left"><font size="2"><?php echo $vf['Key'];?></font></td>

<td align="left"><font size="2"><?php echo $vf['Default'];?></font></td>

<td align="left"><font size="2"><?php echo $vf['Extra'];?></font></td>

<td align="left"><font size="2"><?php echo $vf['Comment'];?></font></td>

</tr>

<?php }?>

</table>

<?php }?>

</body>

</html>


本文出自 “php工程师的博客” 博客,谢绝转载!

你可能感兴趣的:(文档,表结构)