php打印mysql表结构信息


<style type="text/css">
table.hovertable {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #999999;
    border-collapse: collapse;
}
table.hovertable th {
    background-color:#c3dde0;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #a9c6c9;
}
table.hovertable tr {
    background-color:#d4e3e5;
}
table.hovertable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #a9c6c9;
}
</style>
<?php
header('Content-Type: text/html; charset=utf-8');
//mysql配置
$db_host='127.0.0.1';  
$db_username='root';
$db_password='111111';
$db_database = 't111111111111';

$connection=mysql_connect($db_host,$db_username,$db_password);//连接到数据库
mysql_query("set names 'utf8'");//编码转化
if(!$connection){
die("could not connect to the database:</br>".mysql_error());//诊断连接错误

}
$db_selecct=mysql_select_db($db_database);//选择数据库

if(!$db_selecct)
{
die("could not to the database</br>".mysql_error()); 
}
 session_start();

$query="select table_name ,table_comment
from information_schema.tables 
where table_schema='t111111111111'";//构建查询语句
$result=mysql_query($query);//执行查询

while($result_row=mysql_fetch_array($result)){ ?>

<div style="margin:0 auto; width:100%; padding-top:10px;">      
<b class="tableTitle">表名: <?php echo $result_row["table_name"] ?> </b> &nbsp;&nbsp;&nbsp;<?php echo $result_row["table_comment"] ?>
</div>
<table class="hovertable">
    <tr>
        <th width="70">序号</td> 
        <th width="170">字段名</td>
        <th width="140">字段类型</td>   
        <th width="80">允许为空</td> 
        <th width="70">默认值</td>   
        <th>备注</td>   
    </tr>

<?php      
//$sql2 =  "SELECT * FROM COLUMNS WHERE table_name='".$result_row["table_name"]."' ORDER BY COLUMN_NAME";
$sql2 =  "show full fields from ".$result_row["table_name"];

$result2 = mysql_query($sql2);  

$num=0;  
    while($row2=mysql_fetch_array($result2)) { 

$num = $num+1;    ?>
<tr>      
<td><?php echo $num ?></td>       
<td><?php echo $row2["Field"] ?></td>    
<td><?php echo $row2["Type"] ?></td>       
<td><?php echo $row2["Null"] ?></td>    
<td><?php echo $row2["Default"] ?></td>    
<td><?php echo $row2["Comment"] ?></td> 
</tr>

<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
        <td><?php echo $num ?></td>       
        <td><?php echo $row2["Field"] ?></td>    
        <td><?php echo $row2["Type"] ?></td>       
        <td><?php echo $row2["Null"] ?></td>    
        <td><?php echo $row2["Default"] ?></td>    
        <td><?php echo $row2["Comment"] ?></td> 
</tr>
<?php }   ?>
</table> 
<?php }
mysql_close($connection); 
?>

效果图:
1.png

滇ICP备16003699号-2
最后修改:2018 年 11 月 03 日
如果觉得我的文章对你有用,请随意赞赏