Jun 20, 2012

Compare 2 mysql databases

Below code will list all the tables with fields (& type) and total records in each table, so that we can verify whether fields are same in both database fields and record counts.
<?php
mysql_connect("localhost", "user1", "pwd1") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());

$tables = array('tbl1', 'tbl2','tbl3');

echo "<table  border='1' style='float: left'>";
foreach($tables as $tbl){
  $sel = "SELECT COUNT(1) FROM $tbl";
  $res = mysql_query($sel);
  $rec = mysql_fetch_row($res);
 
  echo "<tr><th colspan='2'>$tbl ($rec[0])</th></tr>";
  $sel1 = "SHOW FIELDS FROM $tbl";
  $res1 = mysql_query($sel1) or die($sel1.mysql_error());
  while($rec1 = mysql_fetch_row($res1)){
    echo "<tr><td>$rec1[0]</td><td>$rec1[1]</td></tr>";
  }
}
echo "</table>";
mysql_close();
mysql_connect("localhost", "user2", "pwd2") or die(mysql_error());
mysql_select_db("db2") or die(mysql_error());
echo "<table  border='1' style='float: left'>";
foreach($tables as $tbl){
  $sel = "SELECT COUNT(1) FROM $tbl";
  $res = mysql_query($sel);
  $rec = mysql_fetch_row($res);
 
  echo "<tr><th colspan='2'>$tbl ($rec[0])</th></tr>";
  $sel1 = "SHOW FIELDS FROM $tbl";
  $res1 = mysql_query($sel1) or die($sel1.mysql_error());
  while($rec1 = mysql_fetch_row($res1)){
    echo "<tr><td>$rec1[0]</td><td>$rec1[1]</td></tr>";
  }
}
echo "</table>";
?>

No comments:

Post a Comment

Want to tell something about this post. Please feel free to write...