【PHP应用】mysql数据字典生成器

逻辑

index.php显示初始界面,允许用户输入mysql主机地址、主机端口、数据库名称、mysql用户名、密码等信息;

提交到mysqlDump.php文件,在这里通过一系列的sql语句获取到数据表的各项信息,然后合并归纳出字典信息;


文件介绍

index.php ---  显示程序主页面

mysqlDump.php  --- 接受数据,调用DB.class.php和Utils.class.php来获取数据表信息

DB.class.php ---  数据库操作类

Utils.class.php ---  工具类


代码内容

---index.php
<html>
	<head>
		<meta charset="utf-8"/>
		<title></title>
		<link rel="stylesheet" href="http://getcontent.sinaapp.com//public/css/layout.css" type="text/css" media="all" />
		<link rel="stylesheet" href="http://getcontent.sinaapp.com//public/css/bootstrap.min.css" type="text/css" media="all" />
		<link rel="stylesheet" href="http://getcontent.sinaapp.com//public/css/bootstrap-theme.min.css" type="text/css" media="all" />
	</head>
	<body>
		<div class="zxx_out_box"><div class="zxx_in_box">
		<form class="form-horizontal" role="form" action="./mysqlDump.php" method="post">
		<div class="form-group">
		    <label for="inputEmail3" class="col-sm-2 control-label">mysql 主机地址</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="inputEmail3" name="host" placeholder="host">
		    </div>
		  </div>

		<div class="form-group">
		    <label for="inputPassword3" class="col-sm-2 control-label">主机端口</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="inputPassword3" name="port" placeholder="3306">
		    </div>
		</div>

		<div class="form-group">
		    <label for="inputPassword3" class="col-sm-2 control-label">数据库名称</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="inputPassword3" name="dbname" placeholder="dbname">
		    </div>
		</div>

		<div class="form-group">
		    <label for="inputPassword3" class="col-sm-2 control-label">mysql 用户名</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="inputPassword3" name="username" placeholder="username">
		    </div>
		</div>

		<div class="form-group">
		    <label for="inputPassword3" class="col-sm-2 control-label">登录密码</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="inputPassword3" name="password" placeholder="password">
		    </div>
		</div>

		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <button type="submit" class="btn btn-default">make</button>
		    </div>
		  </div>
		</form>
		</div></dvi>
	</body>
</html>

--mysqlDump.php
//接收提交的参数
$conf['dbHost'] = $_POST['host'];
$conf['dbPort'] = $_POST['port'];
$conf['dbName'] = $_POST['dbname'];
$conf['dbUser'] = $_POST['username'];
$conf['dbPass'] = $_POST['password'];
$conf['dbCharset'] = 'utf8';

//加载工具类和数据库类
require './Untils.class.php';
require './DB.class.php';

//实例化数据库类
$db = new DB($conf);

//获取所有数据表的信息
$tableInfos 	= array();
$tableInfoArr   = $db->showTableStatus();

//循环遍历数据表数组信息,获取每一个数据表的详细字段信息
foreach($tableInfoArr as $tableName=>$oneTable){
	$wordsInfo = Untils::changeKey($db->showFiledsInfo($tableName));
	$tableInfo = Untils::mergeInfoArr($wordsInfo, $oneTable);
	$tableInfos[] = array('name' => $tableName, 'infos' => $tableInfo);
}

require './show.html';

--Utils.class.php
class Untils
{
	/**
	 * 合并查询到的结果
	 * @param  [array] $wordsInfo    [字段信息二维数组]
	 * @param  [array] $tableInfoArr [数据表信息二维数组]
	 * @param  [string] $tableName   [数据表名]
	 * @return [array]    [合并之后的数据表信息数组]
	 */
	public static function mergeInfoArr($wordsInfo, $tableInfoArr){
		return array('words' => $wordsInfo, 'name' => $tableInfoArr['Name'],
			'engine' => $tableInfoArr['Engine'],
			'charset' => $tableInfoArr['Collation'],
			'comment' => $tableInfoArr['Comment'],);
	}
	/**
	 * 提取需要的字段信息,
	 * 并将获取到的字段信息数组的键值替换成需要的名字
	 * @param  [array] $wordsInfo [字段信息数组]
	 * @return [array]            [提取后的字段信息数组]
	 */
	public static function changeKey($wordsInfo){
		$result = array();
		foreach($wordsInfo as $oneWord){
			$result[] = array('name' => $oneWord['Field'], 'type' => $oneWord['Type'], 'comment' => $oneWord['Comment']);
		}
		return $result;
	}
}

--show.html
<html>
<head>
	<title></title>
	<meta keywprd=""/>
	<meta description=""/>
	<meta charset="utf-8"/>
	<link rel="stylesheet" href="http://getcontent.sinaapp.com//public/css/layout.css" type="text/css" media="all" />
	<style type="text/css">
		table.gridtable {
			font-family: verdana,arial,sans-serif;
			font-size:11px;
			color:#333333;
			border-width: 1px;
			border-color: #666666;
			border-collapse: collapse;
			width:600px;
		}
		table.gridtable th {
			border-width: 1px;
			padding: 8px;
			border-style: solid;
			border-color: #666666;
			background-color: #dedede;
		}
		table.gridtable td {
			border-width: 1px;
			padding: 8px;
			border-style: solid;
			border-color: #666666;
			background-color: #ffffff;
		}
	</style>
</head>
<body>
<div class="zxx_out_box" >
	<div class="zxx_in_box" style="margin-left:auto;margin-right:auto;">
	<div class="zxx_header">
        <h1>数据表</h1>
    </div>
	    <div>
			<ul>
				<?php $i = 0;foreach($tableInfos as $oneTable){?><li style="display:inline"><a href="#<?php echo $oneTable['name'];?>"><?php echo $oneTable['name'];if(0 == ++$i % 7){echo '<br/>';}?></a></li>   <?php }?>
			</ul>
		    <?php foreach($tableInfos as $oneTable){?>
			<table class="gridtable" id="<?php echo $oneTable['name'];?>">
				<tr>
					<th colspan="4"><?php echo $oneTable['name'];?></th>
				</tr>
				<tr>
					<td style="border-right-width:0"><?php echo $oneTable['infos']['engine'];?></td>
					<td style="border-width:0"><?php echo $oneTable['infos']['charset'];?></td>
					<td colspan="2" style="border-left-width:0"><?php echo $oneTable['infos']['comment'];?></td>
				</tr>
				<tr>
					<td>名称</td>
					<td>类型</td>
					<td>注释</td>
				</tr>
				<?php foreach($oneTable['infos']['words'] as $oneWords){?>
				<tr>
					<td><?php echo $oneWords['name']?></td>
					<td><?php echo $oneWords['type']?></td>
					<td><?php echo $oneWords['comment']?></td>
				</tr>
				<?php }?>
			</table>
			<br/>
			<?php }?>
		</div>
	</div>
</div>
</body>



--DB.class.php
/**
 * 数据库操作类
 * 执行数据库的增删改查操作
 */
class DB {
    private $dbHost;//数据库主机名
    private $dbName;//数据库名称
    private $dbPort;//数据库端口
    private $username;//数据库用户
    private $password;//数据库密码
    private $charset;//数据库字符集编码
    private $link;//数据库连接资源
    private $error = null;//错误信息
    /**
     * 构造函数
     * 根据传递进来的conf数组给局部变量赋初值
     * 连接数据库
     * @param array $conf
     */
    public function __construct($conf) {
        if(!isset($conf['dbHost']) || !isset($conf['dbName']) || !isset($conf['dbUser']) || !isset($conf['dbPass']) || !isset($conf['dbCharset'])){
            $this->error = '配置不完整';
        }else{
            $this->dbHost  = $conf['dbHost'];
            $this->dbName  = $conf['dbName'];
            $this->dbPort = $conf['dbPort'];
            $this->username = $conf['dbUser'];
            $this->password = $conf['dbPass'];
            $this->charset = $conf['dbCharset'];
            //连接数据库服务器
            $this->link = mysql_connect($this->dbHost . ':' .$this->dbPort,$this->username,$this->password);
            mysql_query("SET NAMES '{$this->charset}'");
            if(false === $this->link){
                $this->error = '连接数据库失败';
            }elseif(!mysql_select_db($this->dbName,$this->link)){//选择数据库
                $this->error = '连接数据库失败';
            }
        }
    }
    /**
     * [获取所有数据表的名称]
     * @return [array] [数据表名称数组]
     */
    public function getAllTableName(){
        $result = mysql_query("SHOW TABLES FROM $this->dbName");
        $tableArr = array();
        while ($row = mysql_fetch_row($result)) {
            $tableArr[] = $row[0];
        }
        return $tableArr;
    }
    /**
     * [获取创建表的sql语句]
     * @param  [string] $tableName [表名]
     * @return [string] [创建表的sql语句]
     */
    public function showCreateTable($tableName){
        $sql = " SHOW CREATE TABLE {$tableName}";
        $resource = mysql_query($sql);
        if(false === $resource){
            $this->error = mysql_error();
            return false;
        }
        return mysql_fetch_assoc($resource);
    }
    /**
     * [获取一个数据表里所有字段的所有信息]
     * @param  [type] $tableName [description]
     * @return [type]            [description]
     */
    public function showFiledsInfo($tableName){
        $sql = "SHOW FULL FIELDS FROM {$tableName};";
        $resource = mysql_query($sql);
        if(false === $resource){
            $this->error = mysql_error();
            return false;
        }
        $result = array();
        while($tmp = mysql_fetch_assoc($resource)){
            $result[] = $tmp;
        }
        return $result;
    }
    /**
     * [获取数据表的状态]
     * @return [type] [description]
     */
    public function showTableStatus(){
        $sql = "show table status from {$this->dbName}";
        $resource = mysql_query($sql);
        if(false === $resource){
            $this->error = mysql_error();
            return false;
        }
        $result = array();
        while($tmp = mysql_fetch_assoc($resource)){
            $result[$tmp['Name']] = $tmp;
        }
        return $result;
    }
    /**
     * 获取错误信息
     * 如果错误信息不存在,则返回null
     * @return string   返回值;返回当前的错误信息
     */
    public function getError(){
        return $this->error;
    }
	public function __destruct(){
		mysql_close($this->link);
	}
}

界面截图

首页


结果页


下载链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值