Guest User

Untitled

a guest
Jun 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. SHOW COLUMNS FROM «table»
  2.  
  3. function columns($table) {
  4.  
  5. $columns = array();
  6. $sql = "desc $table";
  7. $q = mysql_query($sql);
  8.  
  9. while ($r = mysql_fetch_array($q)) {
  10.  
  11. $columns[] = $r[0];
  12.  
  13. }
  14.  
  15. return $columns;
  16.  
  17. }
  18.  
  19. function tables_different($table1, $table2) {
  20.  
  21. $cols1 = columns($table1);
  22. $cols2 = columns($table2);
  23.  
  24. return count(array_diff($cols1, $cols2)) ? true : false;
  25.  
  26. }
  27.  
  28. $sql = "SELECT * FROM tableA WHERE 1"
  29.  
  30. $results = mysql_fetch_assoc($sql);
  31.  
  32. $sql = "truncate table tableB";
  33.  
  34. // run truncate
  35.  
  36. foreach($result as $update){
  37.  
  38. $sql = "Insert into table b VALUES(....)"
  39.  
  40. // run insert
  41. }
  42.  
  43. class MatchTable
  44. {
  45. var $_table_one_name;
  46. var $_table_two_name;
  47.  
  48. var $_table_one_db_user;
  49. var $_table_one_db_pass;
  50. var $_table_one_db_host;
  51. var $_table_one_db_name;
  52.  
  53. var $_table_two_db_user;
  54. var $_table_two_db_pass;
  55. var $_table_two_db_host;
  56. var $_table_two_db_name;
  57.  
  58. var $_table_one_columns = array();
  59. var $_table_two_columns = array();
  60. var $_table_one_types = array();
  61. var $_table_two_types = array();
  62.  
  63. var $_table_one_link;
  64. var $_table_two_link;
  65.  
  66. var $_isTest;
  67.  
  68.  
  69. function MatchTable($isLive = true)
  70. {
  71. $this->_isTest = !$isLive;
  72. }
  73.  
  74. function matchTables($table1, $table2)
  75. {
  76. $this->_table_one_name = $table1;
  77. $this->_table_two_name = $table2;
  78.  
  79. if(isset($this->_table_one_db_pass))
  80. {
  81. $this->db_connect('ONE');
  82. }
  83. list($this->_table_one_columns,$this->_table_one_types) = $this->getColumns($this->_table_one_name);
  84.  
  85. if(isset($this->_table_two_db_pass))
  86. {
  87. $this->db_connect('TWO');
  88. }
  89. list($this->_table_two_columns,$this->_table_two_types) = $this->getColumns($this->_table_two_name);
  90.  
  91. $this->addAdditionalColumns($this->getAdditionalColumns());
  92. }
  93.  
  94. function setTableOneConnection($host, $user, $pass, $name)
  95. {
  96. $this->_table_one_db_host = $host;
  97. $this->_table_one_db_user = $user;
  98. $this->_table_one_db_pass = $pass;
  99. $this->_table_one_db_name = $name;
  100. }
  101.  
  102. function setTableTwoConnection($host, $user, $pass, $name)
  103. {
  104. $this->_table_two_db_host = $host;
  105. $this->_table_two_db_user = $user;
  106. $this->_table_two_db_pass = $pass;
  107. $this->_table_two_db_name = $name;
  108. }
  109.  
  110. function db_connect($table)
  111. {
  112. switch(strtoupper($table))
  113. {
  114. case 'ONE':
  115. $host = $this->_table_one_db_host;
  116. $user = $this->_table_one_db_user;
  117. $pass = $this->_table_one_db_pass;
  118. $name = $this->_table_one_db_name;
  119. $link = $this->_table_one_link = mysql_connect($host, $user, $pass, true);
  120. mysql_select_db($name) or die(mysql_error());
  121. break;
  122. case 'TWO';
  123. $host = $this->_table_two_db_host;
  124. $user = $this->_table_two_db_user;
  125. $pass = $this->_table_two_db_pass;
  126. $name = $this->_table_two_db_name;
  127. $link = $this->_table_two_link = mysql_connect($host, $user, $pass, true);
  128. mysql_select_db($name) or die(mysql_error());
  129. break;
  130. default:
  131. die('Improper parameter in MatchTable->db_connect() expecting "one" or "two".');
  132. break;
  133. }
  134. if (!$link) {
  135. die('Could not connect: ' . mysql_error());
  136. }
  137. }
  138.  
  139. function getColumns($table_name)
  140. {
  141. $columns = array();
  142. $types = array();
  143. $qry = 'SHOW COLUMNS FROM '.$table_name;
  144. $result = mysql_query($qry) or die(mysql_error());
  145. while($row = mysql_fetch_assoc($result))
  146. {
  147. $field = $row['Field'];
  148. $type = $row['Type'];
  149. /*
  150. $column = array('Field' => $field, 'Type' => $type);
  151. array_push($columns, $column);
  152. */
  153. $types[$field] = $type;
  154. array_push($columns, $field);
  155. }
  156. $arr = array($columns, $types);
  157. return $arr;
  158. }
  159.  
  160. function getAdditionalColumns()
  161. {
  162. $additional = array_diff($this->_table_one_columns,$this->_table_two_columns);
  163. return $additional;
  164. }
  165.  
  166. function addAdditionalColumns($additional)
  167. {
  168. $qry = '';
  169. foreach($additional as $field)
  170. {
  171. $qry = 'ALTER TABLE '.$this->_table_two_name.' ADD '.$field.' '.$this->_table_one_types[$field].'; ';
  172.  
  173. if($this->_isTest)
  174. {
  175. echo $qry.'<br><br>';
  176. }
  177. else
  178. {
  179. mysql_query($qry) or die(mysql_error());
  180. }
  181. }
  182. }
  183.  
  184. /**
  185. * End of Class
  186. */
  187. }
Add Comment
Please, Sign In to add comment