Advertisement
Guest User

Untitled

a guest
May 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. <?php
  2. // Change the default setting of the database
  3.  
  4. $dbname = 'your_database_name_here';
  5. $host = 'database_host_here';
  6. $user = 'database_user_here';
  7. $password = 'database_password_here';
  8.  
  9. $options = array(
  10. PDO::ATTR_PERSISTENT => true,
  11. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  12. PDO::ATTR_ORACLE_NULLS => PDO::NULL_TO_STRING
  13. );
  14.  
  15. $dsn = 'mysql:host=' . $host . ';dbname=' . $dbname . ";charset=utf8";
  16. $dbh = new PDO($dsn, $user, $password, $options);
  17.  
  18. // Change the default character set of the database (that does not change any data actually, it's just in case you will create new tables or fields in the future)
  19.  
  20. $sql = "ALTER SCHEMA $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
  21.  
  22. echo "$sql<br>";
  23.  
  24. $stmt = $dbh->prepare($sql);
  25. $stmt->execute();
  26.  
  27. // Get all the fields of all your tables in your database and save the data in an array
  28.  
  29. $sql = "SELECT column_name, data_type, column_type, table_name, collation_name, character_set_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '$dbname' order by table_name";
  30.  
  31. echo "$sql<br>";
  32.  
  33. $stmt = $dbh->prepare($sql);
  34. $fieldarray = $stmt->fetchAll(PDO::FETCH_ASSOC);
  35.  
  36. $table_name_before = "";
  37.  
  38. // Now loop through every field of your database
  39.  
  40. foreach ($fieldarray as $field) {
  41. $collation_name = $field["collation_name"];
  42. $table_name = $field["table_name"];
  43. $character_set_name = $field["character_set_name"];
  44. $column_name = $field["column_name"];
  45. $column_type = $field["column_type"];
  46.  
  47. // For every new table change the default character set of the table (again, this does NOT change any existing data, it is just the default values in case you add new fields later without specifying the character set)
  48.  
  49. if ($table_name != $table_name_before) {
  50. $sql = "ALTER TABLE $table_name CHARACTER SET utf8 COLLATE utf8_unicode_ci";
  51.  
  52. echo "$sql<br>";
  53.  
  54. $stmt = $dbh->prepare($sql);
  55. $stmt->execute();
  56. }
  57.  
  58. // If the collation name or the character set of the field is in latin1, change it to utf8. This ACTUALLY converts the data in the field.
  59. // You might need to change the collation_name and character_set_name variables in case you have other data in your DB than latin1 and latin1_swedish_ci
  60.  
  61. if ($collation_name == 'latin1_swedish_ci' OR $character_set_name == 'latin1') {
  62. $sql = "ALTER TABLE $table_name CHANGE $column_name $column_name $column_type CHARACTER SET utf8 COLLATE utf8_unicode_ci";
  63.  
  64. echo "$sql<br>";
  65.  
  66. $stmt = $dbh->prepare($sql);
  67. $stmt->execute();
  68.  
  69. }
  70.  
  71. $table_name_before = $table_name;
  72.  
  73. }
  74.  
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement