查询数据字典

白与黑 2021-04-24 17:51:00 1206℃ 8009 0条


仅仅作为学习使用,用处不大,可以使用本功能将数据表导出为excel或者Word,在实际开发中还是使用使用专业工具较为稳妥

  • 封装类方法
<?php

namespace datadictionary;

class DataDictionary
{
    protected $database = array();

    public function __construct($database = array())
    {
        $this->database = $database;
        $this->checkParams();
        if(!isset($this->database['DB_PORT'])){
            $this->database['DB_PORT'] = 3306;
        }
    }

    private function checkParams()
    {
        if (!isset($this->database['DB_HOST']) || empty($this->database['DB_HOST']) || !isset($this->database['DB_NAME']) || empty($this->database['DB_NAME']) || !isset($this->database['DB_USER']) || empty($this->database['DB_USER']) || !isset($this->database['DB_PWD']) || empty($this->database['DB_PWD'])) {
            die('<h2 style="text-align: center;color:#f00;">连接参数错误</h2>');
        }
    }

    public function getDictionary()
    {
        header("Content-type:text/html;charset=utf-8");
        ini_set('date.timezone', 'Asia/Shanghai');
        set_time_limit(0);
        $mysql_conn = @mysqli_connect("{$this->database['DB_HOST']}:".intval("{$this->database['DB_PORT']}"), "{$this->database['DB_USER']}", "{$this->database['DB_PWD']}");
        if(mysqli_connect_errno($mysql_conn)){
            die ('<h2 style="text-align: center;"><span style="color:#f00;">对不起,连接错误</span>'.mysqli_connect_error($mysql_conn).'</h2>');
        }
        mysqli_select_db($mysql_conn, $this->database['DB_NAME']);
        $result = mysqli_query($mysql_conn, 'show tables');
        mysqli_query($mysql_conn, "set names " . ((isset($this->database['DB_CHARSET']) && !empty($this->database['DB_CHARSET'])) ? $this->database['DB_CHARSET'] : "utf8"));

        $tables = array();
        // 取得所有表名
        while ($row = mysqli_fetch_array($result)) {
            $tables[]['TABLE_NAME'] = $row[0];
        }

        // 循环取得所有表的备注及表中列消息
        foreach ($tables as $k => $v) {
            $sql = 'SELECT * FROM ';
            $sql .= 'information_schema.TABLES ';
            $sql .= 'WHERE 1 = 1 AND ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$this->database['DB_NAME']}'";
            $table_result = mysqli_query($mysql_conn, $sql);
            while ($t = mysqli_fetch_assoc($table_result)) {
                $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
            }
            $sql = 'SELECT * FROM ';
            $sql .= 'information_schema.COLUMNS ';
            $sql .= 'WHERE 1 = 1 AND ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$this->database['DB_NAME']}'";
            $fields = array();
            $field_result = mysqli_query($mysql_conn, $sql);
            while ($t = mysqli_fetch_assoc($field_result)) {
                $fields[] = $t;
            }
            $tables[$k]['COLUMN'] = $fields;
        }
        mysqli_close($mysql_conn);

        $html = '';

        // 循环所有表
        //print_r($tables);
        foreach ($tables as $k => $v) {
            $html .= '';
            $html .= '';
            $html .= '';
            $html .= '';
            $html .= '<table border="1" cellspacing="0" cellpadding="0" align="center"><caption>表名:' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '</caption><tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>自动递增</th><th>主键</th><th>备注</th></tr>';
            foreach ($v['COLUMN'] as $f) {
                $html .= '';
                $html .= '';
                $html .= '';
                $html .= '';
                $html .= '';
                $html .= '';
                $html .= '';
                $html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td><td class="c2">' . $f['COLUMN_TYPE'] . '</td><td class="c3">' . $f['COLUMN_DEFAULT'] . '</td><td class="c4">' . $f['IS_NULLABLE'] . '</td><td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '是' : ' ') . '</td><td class="c4">' . ($f['COLUMN_KEY'] == 'PRI' ? '主键' : '') . '</td><td class="c6">' . $f['COLUMN_COMMENT'] . '</td></tr>';
            }
            $html .= '</tbody></table><p></p>';
        }

        /* 生成word */
        //header ( "Content-type:application/vnd.ms-word" );
        //header ( "Content-Disposition:attachment;filename={$database['DB_NAME']}数据字典.doc" );


        /* 生成excel*/
        //header ( "Content-type:application/vnd.ms-excel" );
        //header ( "Content-Disposition:attachment;filename={$database['DB_NAME']}数据字典.xls" );

        // 输出
        echo '
          <meta charset="utf-8">
          <title>自动生成数据字典</title>
          <style>    
            body,td,th {font-family:"宋体"; font-size:14px;} 
            table,h1,p{width:960px;margin:0px auto;}        
            table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}        
            table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold;color:#ff039a; }        
            table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:14px; border:1px solid #CCC;padding-left:5px;}        
            table td{height:20px; font-size:14px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}        
            .c1{ width: 150px;}        
            .c2{ width: 150px;}        
            .c3{ width: 80px;}        
            .c4{ width: 100px;}        
            .c5{ width: 100px;}        
            .c6{ width: 300px;}        
          </style>
        ';

        echo '<h1 style="text-align:center;">' . $this->database['DB_NAME'] . '数据字典</h1>';
        echo '<p style="text-align:center;margin:20px auto;">生成时间:' . date('Y-m-d H:i:s') . '</p>';
        echo '<p style="text-align:left;margin:20px auto;font-size:18px;color:#1ed26e;">总共:' . count($tables) . '个数据表</p>';
        echo '';
        echo $html;
    }

    public function __destruct()
    {
        empty($this->database) || $this->database = array();
    }
}
PHP
  • 调用方法
use datadictionary\DataDictionary;

public function dataDictionary()
{
    echo '<style>h2,form{text-align:center;}div{display: inline-block;margin-right:10px;}label{display: inline-block; width:80px;}input{width:150px;height:30px;outline: none;border:1px solid #ccc;font-size:14px;text-indent:5px;margin:10px;}button{outline: none;border:1px solid #bbb;background-color:transparent;height:30px;vertical-align: middle;}</style>';
    echo '<h2>只能连接MySQL数据库,不会记录任何用户信息,请放心使用</h2>';
    echo '<form action="" method="post">';
    echo '<div><label>DB_HOST:</label><input name="DB_HOST" type="text" value="' . input('post.DB_HOST/s', '') . '"></div>';
    echo '<div><label>DB_NAME:</label><input name="DB_NAME" type="text" value="' . input('post.DB_NAME/s', '') . '"></div>';
    echo '<div><label>DB_USER:</label><input name="DB_USER"  type="text" value="' . input('post.DB_USER/s', '') . '"></div>';
    echo '<div><label>DB_PWD:</label><input name="DB_PWD" type="text" value="' . input('post.DB_PWD/s', '') . '"></div>';
    echo '<div><label>DB_PORT:</label><input name="DB_PORT"  type="text" value="'.input('post.DB_PORT/s',3306).'"></div>';
    echo '<button type="submit">确定</button>';
    echo '</form>';
    if (input('post.DB_HOST/s') && input('post.DB_NAME/s') && input('post.DB_USER/s') && input('post.DB_PWD/s')) {
        $database['DB_HOST'] = input('post.DB_HOST/s');
        $database['DB_NAME'] = input('post.DB_NAME/s');
        $database['DB_USER'] = input('post.DB_USER/s');
        $database['DB_PWD']  = input('post.DB_PWD/s');
        $database['DB_PORT'] = input('post.DB_PORT/s');
        $dataDictionary      = new DataDictionary($database);
        $dataDictionary->getDictionary();
    }
}
PHP
  • 效果展示
    效果展示

非特殊说明,本博所有文章均为博主原创。

评论啦~


小小的太阳 - 孙露
00:00
  1. 小小的太阳孙露
  2. 光明谭艳
  3. 我这一生于溪悦
  4. 送亲于溪悦
  5. 我的楼兰云朵
  6. 雨蝶李翊君
  7. 爱拼才会赢叶启田
  8. Warp Me In PlasticCHROMANC
  9. 不谓侠于溪悦
  10. 喝酒喝到吐想你想到哭于溪悦

无歌词,请欣赏。