Advertisement
carbonize

Convert DB to UTF8

Apr 9th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2. // Convert all guestbook tables to UTF-8
  3.  
  4. $include_path = dirname(dirname(__FILE__));
  5. require_once $include_path."/admin/config.inc.php";
  6.  
  7. $DB_HOST = $GB_DB['host'];
  8. $DB_USER = $GB_DB['user'];
  9. $DB_PASSWORD = $GB_DB['pass'];
  10. $DB_DATABASE = $GB_DB['dbName'];
  11.  
  12. $tables = array();
  13. $tables_with_fields = array();
  14.  
  15. echo "<pre>\n";
  16. $link_id = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD) or die('Error establishing a database connection');
  17. echo 'Connected' ."\n";
  18. mysql_select_db($DB_DATABASE, $link_id);
  19. echo 'Selected database' ."\n";
  20.  
  21. echo 'Getting tables:' ."\n";
  22. $resource = mysql_query("SHOW TABLES", $link_id);
  23. while ( $result = mysql_fetch_row($resource) ) {
  24.     if (in_array($result[0], $GB_TBL)) {
  25.         $tables[] = $result[0];
  26.         echo ' - ' . $result[0] ."\n";
  27.     }
  28. }
  29.  
  30. if ( !empty($tables) ) {
  31.     echo 'Starting process' ."\n";
  32.     foreach ( (array) $tables as $table ) {
  33.         echo 'Working on table "' . $table . '"';
  34.         $resource = mysql_query("EXPLAIN $table", $link_id);
  35.         while ( $result = mysql_fetch_assoc($resource) ) {
  36.             if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
  37.                 $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " .  ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
  38.                 echo '.';
  39.         }
  40.         echo "\n";
  41.     }
  42.  
  43.     // Change all text/string fields of the tables to their corresponding binary text/string representations.
  44.     echo 'Altering tables to binary character set';
  45.     foreach ( (array) $tables as $table ) {
  46.         mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
  47.         echo '.';
  48.     }
  49.     echo "\n";
  50.  
  51.     // Change database and tables to UTF-8 Character set.
  52.     echo 'Altering tables to utf8 character set';
  53.     mysql_query("ALTER DATABASE " . $DB_DATABASE . " CHARACTER SET utf8", $link_id);
  54.     foreach ( (array) $tables as $table ) {
  55.         mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
  56.         echo '.';
  57.     }
  58.     echo "\n";
  59.  
  60.     // Return all binary text/string fields previously changed to their original representations.
  61.     echo 'Altering binary text/string fields to original representation';
  62.     foreach ( (array) $tables_with_fields as $table => $fields ) {
  63.         foreach ( (array) $fields as $field_type => $field_options ) {
  64.             mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
  65.         }
  66.         echo '.';
  67.     }
  68.     echo "\n";
  69.  
  70.     // Optimize tables and finally close the mysql link.
  71.     echo 'Optimizing tables' . "\n";
  72.     foreach ( (array) $tables as $table )
  73.         mysql_query("OPTIMIZE TABLE $table", $link_id);
  74.     mysql_close($link_id);
  75.     echo "DONE\n\n";
  76.     echo "Please remove this script from your server!";
  77. } else {
  78.     die('There are no tables?');
  79. }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement