PHP生成Mysql数据库表结构
#wiki
<?/*=======================================
** date:2010-3-9
** 生成mysql数据库结构报表
=======================================*/
if($_POST){
if(!$link = mysql_connect($_POST['host'], $_POST['user'], $_POST['password'])){
echo '不能连接数据库';
exit;
}
mysql_select_db($_POST['dbname']);
mysql_query("set names 'utf8'");
$result = mysql_query("SHOW TABLE STATUS");
while ($row = mysql_fetch_assoc($result)) {
$table[$row['Name']]['comment'] = $row['Comment'];
}
foreach($table as $key=>$value){
$fields = mysql_query("show full fields from ".$key);
while($row=mysql_fetch_assoc($fields))
{
$table[$key]['field'][$row['Field']]['key'] = $row['Key'];
$table[$key]['field'][$row['Field']]['type'] = $row['Type'];
$table[$key]['field'][$row['Field']]['default'] = $row['Default'];
$table[$key]['field'][$row['Field']]['null'] = $row['Null'];
$table[$key]['field'][$row['Field']]['comment'] = $row['Comment'];
}
}
mysql_close();
}
?>
<!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">
<html>
<head>
<title> 数据库 </title>
<meta http-equiv=content-type content="text/html; charset=utf-8">
<style type="text/css">
.title {
font-size: 14px;
color: #FFF;
background-color: #333;
height: 25px;
}
</style>
</head>
<body>
<form method=post action="">
数据库地址:<input type="text" name="host" value='localhost'><br>
用 户 名:<input type="text" name="user" value='root'><br>
密 码:<input type="password" name="password"><br>
数据库名称:<input type="text" name="dbname"><br>
<input type="submit">
</form>
<?
if($table){
echo "========================================================================<br>";
foreach($table as $key=>$fields){
echo <<<EOT
<table width="50%" border="1" cellspacing="0" cellpadding="0">
<tr align="center" class="title">
<td colspan="6">{$key}({$fields['comment']})</td>
</tr>
<tr align="center">
<td>字段</td>
<td>类型</td>
<td>默认</td>
<td>主键</td>
<td>空</td>
<td>说明</td>
</tr>
EOT;
foreach($fields['field'] as $fkey=>$finfo){
echo <<<EOT
<tr align="center">
<td> $fkey</td>
<td> {$finfo['type']}</td>
<td> {$finfo['default']}</td>
<td> {$finfo['key']}</td>
<td> {$finfo['null']}</td>
<td> {$finfo['comment']}</td>
</tr>
EOT;
}
echo "</table><br>";
}
}
?>
</body>
</html>
生成样式如下
评论