zoukankan      html  css  js  c++  java
  • 数据库比对脚本(PHP版)

    $config = [
        'hotfix' => [
            'host'=>'',
            'port'=>'',
            'account'=>'',
            'password'=>'',
            'database'=>'',
        ],
        'release' => [
            'host'=>'',
            'port'=>'',
            'account'=>'',
            'password'=>'',
            'database'=>'',
        ],
        'master' => [
            'host'=>'',
            'port'=>'',
            'account'=>'',
            'password'=>'',
            'database'=>'',
        ],
    ];
    
    function display($compare1,$compare2){
        $html = '<title>数据库对比脚本</title>';
        $html .= '<form method="GET">
                原数据库:
                <select name="db1">
                    <option value="hotfix"'.($compare1=='hotfix'?'selected="selected"':"").'>开发站</option>
                    <option value="release"'.($compare1=='release'?'selected="selected"':"").'>测试站</option>
                    <option value="master"'.($compare1=='master'?'selected="selected"':"").'>正式站</option>
                </select>
                现数据库:
                <select name="db2">
                    <option value="hotfix"'.($compare2=='hotfix'?'selected="selected"':"").'>开发站</option>
                    <option value="release"'.($compare2=='release'?'selected="selected"':"").'>测试站</option>
                    <option value="master"'.($compare2=='master'?'selected="selected"':"").'>正式站</option>
                </select>
                <input type="submit" value="对比" /><br><br>';    
        $html .= '相比原数据库,现数据库:<br>';
        return $html;
    }
    
    function databaseData($config,$compare){
        $aaaaa = mysql_connect(
            $config[$compare]['host'].':'.$config[$compare]['port'],
            $config[$compare]['account'],
            $config[$compare]['password']
        );
        mysql_select_db($config[$compare]['database']); //标准的数据库
        $q = mysql_query("show tables");
        while($s = mysql_fetch_array($q)){
            $name = $s[0];
            $q1 = mysql_query("desc $name");
            while ($s1 = mysql_fetch_array($q1)) {
                $a[$name]['field'][] =$s1[0];
                $a[$name]['format'][] =$s1[1];
            }
        }
        mysql_close();
        return $a;
    }
    
    function dataCompare($a,$aa){
        $f = $e = array();
        $str = $fuhao ='';
        foreach($a as $k=>$v){
            if(!is_array($aa[$k])){
                $e[] = $k;
                echo "<div style='background:#fdd'>缺少表:<font color='blue'>$k</font></div>";
            }
            else{
                foreach($v['field'] as $k1=>$v1){
                    if(!in_array($v1,$aa[$k]['field'])){
                        $f[$k]['field'][] = $v1;
                        echo "<div style='background:#ff9'>表<font color='blue'>".$k."</font>缺少字段: <font color='red'>".$v1."</font>"."</div>";
                    }else{
                        if(!in_array($v['format'][$k1],$aa[$k]['format'])){
                            $f[$k]['format'][] = $v['format'][$k1];
                            echo "<div style='background:#ddd'>表<font color='blue'>".$k."</font>字段 <font color='red'>".$v1."</font>修改为<font color='green'>".$v['format'][$k1]."</font>"."</div>";
                        }
                    }
                }
            }
        }
    }
    
    $compareOne = isset($_GET['db1'])?$_GET['db1']:'';
    $compareTwo = isset($_GET['db2'])?$_GET['db2']:'';
    echo display($compareOne,$compareTwo);
    $a = databaseData($config,$compareOne);
    $aa = databaseData($config,$compareTwo);
    dataCompare($a,$aa);
  • 相关阅读:
    ES5、6对异步事件的处理方式
    SQL技巧
    前端技巧
    docker start 启动失败,logs 没有日志
    mysql使用存储过程insert
    Spring 手动回滚事务/提交事务,及通过
    mysql触发器trigger 实例详解
    @PostConstruct 之NullException
    springboot 2 多数据源 hikari 连接池
    swagger 日期Date
  • 原文地址:https://www.cnblogs.com/boystar/p/6101407.html
Copyright © 2011-2022 走看看