zoukankan      html  css  js  c++  java
  • php实现MySQL两库对比升级版

    define('DATABASE1', 'db1');
    $dbi1 = new DbMysql;
    $dbi1->dbh = 'mysql://root:password@127.0.0.1/'.DATABASE1;
    
    define('DATABASE2', 'db2');
    $dbi2 = new DbMysql;
    $dbi2->dbh = 'mysql://root:password@127.0.0.1/'.DATABASE2;
    
    // db1
    $db1 = array();
    $map = array();
    $dbi1->fetchMap("SHOW TABLES", $map);
    $tables = array_keys($map);
    for($i=0; $i<count($tables); $i++){
        $map = array();
        $dbi1->fetchMap("DESCRIBE ".$tables[$i], $map);
        $structures = array();
        foreach($map as $k=>$v){
            $structures[] = "$k=$v";
        }
        $db1[$tables[$i]] = $structures;
    }
    
    // db2
    $db2 = array();
    $map = array();
    $dbi2->fetchMap("SHOW TABLES", $map);
    $tables = array_keys($map);
    for($i=0; $i<count($tables); $i++){
        $map = array();
        $dbi2->fetchMap("DESCRIBE ".$tables[$i], $map);
        $structures = array();
        foreach($map as $k=>$v){
            $structures[] = "$k=$v";
        }
        $db2[$tables[$i]] = $structures;
    }
    
    // db1 compare db2
    echo("***** ".DATABASE1." *****
    ");
    foreach($db2 as $table=>$structures2){
        $structures1 = $db1[$table];
        if(empty($structures1)) {
            echo(".$table not exist
    ");
            continue;
        }
        for($i=0; $i<count($structures2); $i++){
            if(!in_array($structures2[$i], $structures1))
                echo $table.".".$structures2[$i]."
    ";
        }
    }
    
    // db2 compare db1
    echo("***** ".DATABASE2." *****
    ");
    foreach($db1 as $table=>$structures1){
        $structures2 = $db2[$table];
        if(empty($structures2)) {
            echo(DATABASE2.".$table not exist
    ");
            continue;
        }
        for($i=0; $i<count($structures1); $i++){
            if(!in_array($structures1[$i], $structures2))
                echo $table.".".$structures1[$i]."
    ";
        }
    }
  • 相关阅读:
    重温 JSP 与 Servlet
    Web/Java Web项目如何模块化?没有正文,别点
    Struts2 In Action笔记_页面到动作的数据流入和流出
    SQL小知识_长期总结
    Java代码工具箱_用Set给List/Vector去重
    Oracle小知识_长期总结
    样式缩写——css技巧(一)
    CSS中强大的EM
    人生苦短,我用python!
    15个超级实用的jQuery插件
  • 原文地址:https://www.cnblogs.com/coffee_cn/p/7998610.html
Copyright © 2011-2022 走看看