PHP 进行数据庫对比工具

<?php
/**
  * author jackluo
  * [email protected]
  */
class IMysqlDiff
{

    
    private $master,$slave;
    

    public function __construct()
    {
        $this->master = array();
        $this->slave = array();
    }

    /**
     *    master
     */
    
    public function setMasterTable($host,$user,$pwd,$dbname){

        $conn      = mysql_connect($host,$user,$pwd); 
        if (!$conn) {
            throw new Exception("Database Connect Error");
        }

        $select  = mysql_select_db($dbname,$conn); //标准的数据库 
        if (!$select) {
            throw new Exception("SELECT Database Error");    
        }
        
        $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)) { 
                $this->master[$name][] =$s1[0]; 
            } 
        } 
        mysql_close($conn);
    }
    

    /**
     *    Slave 
     */
    
    public function setSlaveTable($host,$user,$pwd,$dbname){
        $conn       = mysql_connect($host,$user,$pwd); 
        if (!$conn) {
            throw new Exception("Database Connect Error");
        }

        $select  = mysql_select_db($dbname,$conn); //标准的数据库 
        if (!$select) {
            throw new Exception("SELECT Database Error");    
        }
        
        $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)) { 
                $this->slave[$name][] =$s1[0]; 
            } 
        } 
        mysql_close($conn);
    }

    /**
     *    comparison
     */
    
    public function comparison(){
        $f = $e = array(); 
        $str = $fuhao =‘‘; 
        foreach($this->master as $k=>$v){ 
            
            if (!empty($this->slave[$k])){
                if (!is_array($this->slave[$k])) {
                    $e[] = $k;     //缺少表 
                }else if(count($this->slave[$k]) <> count($v)){
                    foreach($v as $k1=>$v1){ 
                        if(!in_array($v1,$this->slave[$k])){ 
                            $f[$k][] = $v1; ////缺少表的字段 
                        } 
                    }
                }
            }else{
                $e[] = $k;
            }
        }
        return array(‘tables‘=>$e,‘fields‘=>$f);
    }
    
}

/*
$diff = new IMysqlDiff();
$diff->setMasterTable(‘127.0.0.1‘,‘root‘,‘admin‘,‘platform_test‘);
$diff->setSlaveTable(‘127.0.0.1‘,‘root‘,‘admin‘,‘platform‘);
$result =     $diff->comparison();

print_r($result);
//*/
//*/

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。