Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. $truncate=true;
  2.  
  3. /**
  4. * DB Server Credentials - Needs to have full access to both databases
  5. */
  6. $dbHost='localhost';
  7. $dbUser='root';
  8. $dbPass='password';
  9.  
  10. /** Tables to Synch
  11. */
  12. $tablesToSynch = array(
  13. 'categories',
  14. 'categories_description',
  15. 'products',
  16. 'products_description',
  17. 'products_to_categories',
  18. 'manufacturers',
  19. );
  20.  
  21. $dbOld = 'dbold';
  22.  
  23. $dbNew = 'dbnew';
  24.  
  25.  
  26. /*********** CODE BELOW HERE - NO NEED TO EDIT UNLESS YOU WANT TO ***********/
  27. $_conn = mysql_connect($dbHost,$dbUser,$dbPass) or die('DB Connection Failed');
  28.  
  29. $dbOldTables= fetch_tables($dbOld);
  30.  
  31. $dbNewTables = fetch_tables($dbNew);
  32.  
  33.  
  34. foreach($tablesToSynch as $table){
  35.  
  36. echo '<h3>Doing '.$table.'</h3>';
  37. //check for common tables
  38. if(in_array($table, $dbOldTables) && in_array($table, $dbNewTables)){
  39. //now get column data
  40. $dbOldTableCols = fetch_columns($dbOld,$table);
  41.  
  42. $dbNewTableCols = fetch_columns($dbNew,$table);
  43.  
  44. //now for the column comparison
  45. $cols=array_intersect($dbOldTableCols, $dbNewTableCols);
  46.  
  47. //now emptying the new DB if set to do so
  48. if($truncate){
  49. db_query("TRUNCATE $dbNew.$table");
  50. }
  51.  
  52. //copy old table to new DB so we can copy columns
  53. $tempTable=copy_table($dbOld, $dbNew, $table);
  54.  
  55. //now build SQL and run
  56. $sql = "insert into $dbNew.$table (" . implode(', ',$cols) . ") select " . implode(', ',$cols) . " from $dbNew.$tempTable";
  57. db_query($sql);
  58.  
  59. //now drop temp table
  60. db_query("DROP TABLE $dbNew.$tempTable");
  61. }
  62.  
  63. }
  64.  
  65. /****** FUNCTIONS ********/
  66.  
  67. function db_query($query){
  68. $output = mysql_query($query) or die('
  69. <h1 style="color: red">Uh Oh......MySQL Error:</h1>
  70. <h3>Query:</h3>
  71. <pre>' . htmlentities($query) . '</pre>
  72. <h3>MySQL Error:</h3>
  73. ' . mysql_error() . '
  74. <hr /> <hr />'); return $output;
  75. }
  76.  
  77.  
  78. function fetch_tables($dbname){
  79. $query=db_query("show tables from $dbname");
  80. while($r=mysql_fetch_assoc($query)){
  81. $return[]=$r["Tables_in_$dbname"];
  82. }
  83. return $return;
  84. }
  85.  
  86. function fetch_columns($dbname, $table){
  87. $query = db_query("SHOW COLUMNS from $dbname.$table");
  88. while($r= mysql_fetch_assoc($query)){
  89. $return[]=$r['Field'];
  90. }
  91. return $return;
  92. }
  93.  
  94. function copy_table($fromDb, $toDb, $table){
  95. db_query("DROP TABLE IF EXISTS $toDb.temp_$table");
  96. db_query("CREATE TABLE $toDb.temp_$table LIKE $fromDb.$table");
  97. db_query("ALTER TABLE $toDb.temp_$table DISABLE KEYS");
  98. db_query("INSERT INTO $toDb.temp_$table SELECT * FROM $fromDb.$table");
  99. db_query("ALTER TABLE $toDb.temp_$table ENABLE KEYS");
  100. return "temp_$table";
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement