Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0); // for bigger databases
  4.  
  5. $host = '';
  6. $user = '';
  7. $pass = '';
  8. $dbname = '';
  9.  
  10. $new_collation = 'utf8_general_ci';
  11.  
  12. $convert_string = 'convert(cast(convert(%s using latin1) as binary) using utf8)';
  13. $alter_table_and_all_columns = 'alter table %s convert to character set utf8 collate '.$new_collation;
  14.  
  15. $conn = mysqli_connect($host,$user,$pass,$dbname);
  16.  
  17. $sql = "SHOW TABLES";
  18. $result = mysqli_query($conn,$sql);
  19.  
  20. while($row = mysqli_fetch_assoc($result)) {
  21. $table_list[] = $row['Tables_in_'.$dbname];
  22. }
  23.  
  24. foreach($table_list as $table) {
  25. $res = mysqli_query($conn,"SHOW CREATE TABLE $table");
  26.  
  27. $row = mysqli_fetch_array($res);
  28.  
  29. #echo $row[0] . "<br>\n";
  30.  
  31. $foo = mysqli_query($conn, "SHOW FULL COLUMNS FROM $table");
  32.  
  33. $set = [];
  34.  
  35. while ($row1 = mysqli_fetch_array($foo)) {
  36. if (!empty($row1['Collation'])) {
  37. # echo $row1['Field'] . ' ' . $row1['Collation'] . "\n";
  38. $set[] = $row1['Field'];
  39. }
  40. }
  41.  
  42. $sql1 = "UPDATE $table SET ";
  43.  
  44. $sets = [];
  45.  
  46. foreach($set as $field) {
  47. $sets[] = $field.'='.sprintf($convert_string,$field);
  48. }
  49. $sql1 .= implode(",",$sets);
  50. $res1 = mysqli_query($conn,$sql1);
  51.  
  52. if (preg_match('#CHARSET=latin1#msi', $row[1])) {
  53. $res2 = mysqli_query($conn, sprintf($alter_table_and_all_columns, $table));
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement