数据库比对脚本(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);

 

posted @ 2016-11-25 14:13  boystar  阅读(1145)  评论(0编辑  收藏  举报