Guest User

Untitled

a guest
Jun 29th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. <?php
  2.  
  3. function MysqlError()
  4. {
  5. if (mysql_errno())
  6. {
  7. echo "<b>Mysql Error: " . mysql_error() . "</b>n";
  8. }
  9. }
  10.  
  11. $username = "root";
  12. $password = "";
  13. $db = "database";
  14. $host = "localhost";
  15.  
  16. $target_charset = "utf8";
  17. $target_collate = "utf8_general_ci";
  18.  
  19. echo "<pre>";
  20.  
  21. $conn = mysql_connect($host, $username, $password);
  22. mysql_select_db($db, $conn);
  23.  
  24. $tabs = array();
  25. $res = mysql_query("SHOW TABLES");
  26. MysqlError();
  27. while (($row = mysql_fetch_row($res)) != null)
  28. {
  29. $tabs[] = $row[0];
  30. }
  31.  
  32. // now, fix tables
  33. foreach ($tabs as $tab)
  34. {
  35. $res = mysql_query("show index from {$tab}");
  36. MysqlError();
  37. $indicies = array();
  38.  
  39. while (($row = mysql_fetch_array($res)) != null)
  40. {
  41. if ($row[2] != "PRIMARY")
  42. {
  43. $indicies[] = array("name" => $row[2], "unique" => !($row[1] == "1"), "col" => $row[4]);
  44. mysql_query("ALTER TABLE {$tab} DROP INDEX {$row[2]}");
  45. MysqlError();
  46. echo "Dropped index {$row[2]}. Unique: {$row[1]}n";
  47. }
  48. }
  49.  
  50. $res = mysql_query("DESCRIBE {$tab}");
  51. MysqlError();
  52. while (($row = mysql_fetch_array($res)) != null)
  53. {
  54. $name = $row[0];
  55. $type = $row[1];
  56. $set = false;
  57. if (preg_match("/^varchar((d+))$/i", $type, $mat))
  58. {
  59. $size = $mat[1];
  60. mysql_query("ALTER TABLE {$tab} MODIFY {$name} VARBINARY({$size})");
  61. MysqlError();
  62. mysql_query("ALTER TABLE {$tab} MODIFY {$name} VARCHAR({$size}) CHARACTER SET {$target_charset}");
  63. MysqlError();
  64. $set = true;
  65.  
  66. echo "Altered field {$name} on {$tab} from type {$type}n";
  67. }
  68. else if (!strcasecmp($type, "CHAR"))
  69. {
  70. mysql_query("ALTER TABLE {$tab} MODIFY {$name} BINARY(1)");
  71. MysqlError();
  72. mysql_query("ALTER TABLE {$tab} MODIFY {$name} VARCHAR(1) CHARACTER SET {$target_charset}");
  73. MysqlError();
  74. $set = true;
  75.  
  76. echo "Altered field {$name} on {$tab} from type {$type}n";
  77. }
  78. else if (!strcasecmp($type, "TINYTEXT"))
  79. {
  80. mysql_query("ALTER TABLE {$tab} MODIFY {$name} TINYBLOB");
  81. MysqlError();
  82. mysql_query("ALTER TABLE {$tab} MODIFY {$name} TINYTEXT CHARACTER SET {$target_charset}");
  83. MysqlError();
  84. $set = true;
  85.  
  86. echo "Altered field {$name} on {$tab} from type {$type}n";
  87. }
  88. else if (!strcasecmp($type, "MEDIUMTEXT"))
  89. {
  90. mysql_query("ALTER TABLE {$tab} MODIFY {$name} MEDIUMBLOB");
  91. MysqlError();
  92. mysql_query("ALTER TABLE {$tab} MODIFY {$name} MEDIUMTEXT CHARACTER SET {$target_charset}");
  93. MysqlError();
  94. $set = true;
  95.  
  96. echo "Altered field {$name} on {$tab} from type {$type}n";
  97. }
  98. else if (!strcasecmp($type, "LONGTEXT"))
  99. {
  100. mysql_query("ALTER TABLE {$tab} MODIFY {$name} LONGBLOB");
  101. MysqlError();
  102. mysql_query("ALTER TABLE {$tab} MODIFY {$name} LONGTEXT CHARACTER SET {$target_charset}");
  103. MysqlError();
  104. $set = true;
  105.  
  106. echo "Altered field {$name} on {$tab} from type {$type}n";
  107. }
  108. else if (!strcasecmp($type, "TEXT"))
  109. {
  110. mysql_query("ALTER TABLE {$tab} MODIFY {$name} BLOB");
  111. MysqlError();
  112. mysql_query("ALTER TABLE {$tab} MODIFY {$name} TEXT CHARACTER SET {$target_charset}");
  113. MysqlError();
  114. $set = true;
  115.  
  116. echo "Altered field {$name} on {$tab} from type {$type}n";
  117. }
  118.  
  119. if ($set)
  120. mysql_query("ALTER TABLE {$tab} MODIFY {$name} COLLATE {$target_collate}");
  121. }
  122.  
  123. // re-build indicies..
  124. foreach ($indicies as $index)
  125. {
  126. if ($index["unique"])
  127. {
  128. mysql_query("CREATE UNIQUE INDEX {$index["name"]} ON {$tab} ({$index["col"]})");
  129. MysqlError();
  130. }
  131. else
  132. {
  133. mysql_query("CREATE INDEX {$index["name"]} ON {$tab} ({$index["col"]})");
  134. MysqlError();
  135. }
  136.  
  137. echo "Created index {$index["name"]} on {$tab}. Unique: {$index["unique"]}n";
  138. }
  139.  
  140. // set default collate
  141. mysql_query("ALTER TABLE {$tab} DEFAULT CHARACTER SET {$target_charset} COLLATE {$target_collate}");
  142. }
  143.  
  144. // set database charset
  145. mysql_query("ALTER DATABASE {$db} DEFAULT CHARACTER SET {$target_charset} COLLATE {$target_collate}");
  146.  
  147. mysql_close($conn);
  148. echo "</pre>";
  149.  
  150. mysql --database=dbname -B -N -e "SHOW TABLES"
  151. | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}'
  152. | mysql --database=dbname &
  153.  
  154. <?php
  155. // your connection
  156. mysql_connect("localhost","root","***");
  157. mysql_select_db("db1");
  158.  
  159. // convert code
  160. $res = mysql_query("SHOW TABLES");
  161. while ($row = mysql_fetch_array($res))
  162. {
  163. foreach ($row as $key => $table)
  164. {
  165. mysql_query("ALTER TABLE " . $table . " CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci");
  166. echo $key . " => " . $table . " CONVERTED<br />";
  167. }
  168. }
  169. ?>
Add Comment
Please, Sign In to add comment