Встала необходимость почистить сервер MySQL от старых баз. Их скопилось очень много. так же было замечено, что в разных базах (аля test2, test3 и т.д.) хранятся "на глаз" идентичные таблицы. Как же увидеть разницу между таблицами? Вот если бы было решение, которое показывало разницу между структурой таблиц в MySQL.
<?php
/*
Created by...........: Emil Maran (maran-emil.de)
Release type.........: Script PHP/mySQL
Price................: Freeware
*/
#######################################################################################################
function buildArrayDB1($sDB){
global $link;
$db_selected = mysql_select_db($sDB, $link);
$sql = "SHOW TABLES FROM $sDB";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
//echo "{$row[0]} <br>";
$table = $row[0];
$arTotalDB[$sDB][$table] = buildArrayFields($table);
}
return $arTotalDB;
}
function buildArrayDB2($sDB){
global $link;
$db_selected = mysql_select_db($sDB, $link);
$sql = "SHOW TABLES FROM $sDB";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
//echo "{$row[0]} <br>";
$table = $row[0];
$arTotalDB[$sDB][$table] = buildArrayFields($table);
}
return $arTotalDB;
}
#######################################################################################################
function buildArrayFields($table){
$result = mysql_query("SHOW COLUMNS FROM {$table}");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$arTableFields[] = $row;
}
}
return $arTableFields;
}
/*
while(list($key,$val)=each($arTableFields)){
echo $key."--".$val."<BR>";
}
*/
#######################################################################################################
function compareDB($sDB1,$sDB2){
global $arDB1,$arDB2;
$arDB1 = buildArrayDB1($sDB1);
$arDB2 = buildArrayDB2($sDB2);
echo "<B>Compare:<BR> ($sDB1) with ($sDB2)</B> <br><br>";
if(is_array($arDB1[$sDB1])){
foreach($arDB1[$sDB1] as $key=>$val){
//echo $key."--".print_r($val)."<BR>";
echo "<div>";
// check if the table name is the identical
if((trim($arDB1[$sDB1][$key]))==(trim($arDB2[$sDB2][$key]))) {
echo "<FONT COLOR='green'><b>".$key."</b> is present in $sDB2 </FONT><BR>";
// check if are same number of fields
if((count($arDB1[$sDB1][$key]))==(count($arDB2[$sDB2][$key]))){
echo "<B>IDENTICAL</B><BR>";
}
else{
echo "<B>NOT IDENTICAL :: COLUMNS: </B><HR>";
//compareFields($arDB1[$sDB1][$key]);
foreach($arDB1[$sDB1][$key] as $column){
echo "<B>".$column ['Field']."</B> ".$column ['Type']."<br>";
}
//print '<pre>'; print_r($arDB1[$sDB1][$key]); print '</pre>';
}
}
else {
echo "<FONT COLOR='red'><b>".$key."</b> is missing from $sDB2</FONT><BR>";
}
echo "</div>";
}
}
}
#######################################################################################################
function compareFields($table){
global $arDB1,$arDB2,$sDB1,$sDB2;
// if are not identical show fields
foreach($table as $keyf=>$valf){
if((trim($arDB1[$key][$keyf]['Field']))===(trim($arDB2[$key][$keyf]['Field']))){
//if(isset($table[$keyf]['Field'])){
print "<font color='blue'>".$table[$keyf]['Field']." $bad</font><BR>";
} else {
print "<font color='red'>".$table[$keyf]['Field']." $bad</font><BR>";
}
}
}
#######################################################################################################
function checkConnectionAndDB(){
global $sDB1,$sDB2,$dbservername,$dbserveruser,$dbserverpass;
if(
(!empty($_POST["sDB1"]))&&
(!empty($_POST["sDB2"]))&&
(!empty($_POST["dbservername"]))&&
(!empty($_POST["dbserveruser"]))
){
$sDB1 = $_POST["sDB1"];
$sDB2 = $_POST["sDB2"];
/* DEFAULT CONNECTION SETTINGS */
$dbservername = $_POST["dbservername"];
$dbserveruser = $_POST["dbserveruser"];
$dbserverpass = $_POST["dbserverpass"];
//print_r($_POST);
return "valid";
}
else{
/* DEFAULT DEFINE 2 DB FOR COMPARE */
$sDB1 = "bestprice";
$sDB2 = "bestpricee";
//$sDB1prefix = "";
//$sDB2prefix = "";
/* DEFAULT CONNECTION SETTINGS */
$dbservername = "localhost";
$dbserveruser = "root";
$dbserverpass = "";
return "notvalid";
}
}
?>
<style>
td {font: 11px tahoma; vertical-align: top;padding:10px}
div {border: 1px solid #999999; padding: 10px}
</style>
<TABLE style="padding:10px">
<TR>
<TD>
<form action="<?=$_SERVER["REQUEST_URI"]?>" method="post">
<input type="text" name="sDB1" value="<?=$_POST["sDB1"]?>"> DB NAME 1<BR>
<input type="text" name="sDB2" value="<?=$_POST["sDB2"]?>"> DB NAME 2<BR>
<input type="text" name="dbservername" value="<?=$_POST["dbservername"]?>"> SERVER NAME<BR>
<input type="text" name="dbserveruser" value="<?=$_POST["dbserveruser"]?>"> SERVER USERNAME<BR>
<input type="text" name="dbserverpass" value="<?=$_POST["dbserverpass"]?>"> SERVER PASSWORD<BR>
<input type="submit" value="Comapre">
</form>
</TD>
<TD>
<?
$valid_data = checkConnectionAndDB();
if($valid_data=="valid"){
$link = mysql_connect($dbservername,$dbserveruser, $dbserverpass);
compareDB($sDB1,$sDB2);
}
?>
</TD>
<TD>
<?
if($valid_data=="valid"){
compareDB($sDB2,$sDB1);
}
?>
</TD>
</TR>
</TABLE>
<pre>
<?
/*
print_r($arDB1);
print_r($arDB2); die();
*/
?>
</pre>