mysql database difference comparison PHP code

  • 2020-05-12 02:21:36
  • OfStack

Of course, you can go to PHPMYADMIN and browse for 1. But there are omissions. So I wrote a script to compare the database differences (PHP), which is convenient for me. Of course, the code is very simple, I will not explain, paste the code:
 
<? 
mysql_connect('localhost','root','root'); 
mysql_select_db('tablea'); // Standard 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][] =$s1[0]; 
} 
} 
mysql_close(); 
mysql_connect('localhost','root','root'); 
mysql_select_db('tableb');// The database that needs to be compared  
$q2 = mysql_query("show tables"); 
while($s2 = mysql_fetch_array($q2)){ 
$name2= $s2[0]; 
$q3 = mysql_query("desc $name2"); 
while ($s3 = mysql_fetch_array($q3)) { 
$aa[$name2][] =$s3[0]; 
} 
} 
mysql_close(); 
$f = $e = array(); 
$str = $fuhao =''; 
foreach($a as $k=>$v){ 
if(!is_array($aa[$k])){ 
$e[] = $k; 
} 
else{ 
if(count($aa[$k]) <> count($v)){ 
foreach($v as $k1=>$v1){ 
if(!in_array($v1,$aa[$k])){ 
$f[$k][] = $v1; 
} 
} 
} 
} 
} 
echo "<pre>"; 
print_r($e);// Lack of table  
print_r($f);// Missing table fields  
?> 

Related articles: