latin1_swedish_ci to utf8_general_ci

从旧主机搬到新主机最烦人的就是乱码问题。这也是个由来已久的问题了。
wordpress 2.2开始,wordpress提供DB_CHARSET和DB_COLLATE这两个参数以便我们更好地解决这个问题。但似乎这些并不够,仍旧会产生这样那样的问题,如Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\wordpress\wp-includes\classes.php on line 88。最根本的解决应该还是把数据库里的数据由latin1_swedish_ci格式转换成utf8_general_ci格式。
wordpress官方的论坛就像wordpress那样,很有人气。你遇到的问题,很可能里面已经有人讨论过并已经解决了。乱码问题这个很热门的问题,当然也是完美地解决了地。
以下方法涉及数据库修改,请在备份数据库后操作!
解决的方法是这样的:
1. 下载g30rg3_x提供的这个plugin;
2. 确保你的wordpress是2.2.x或2.1.x,因为这个plugin只支持这两个系列的版本;
3. 上传到plugins文件夹并激活;
4. 到’UTF-8 Database Converter’菜单中按照提示进行剩余操作即可。

上面的办法多了不少的限制,如wordpress的版本需是2.2.x或2.1.x,并能保证你的wordpress还能登录!(是的,很可能你的wordpress这时已经不能登录了。)
这时我们可以借用一下g30rg3_x提供的UTF8_DB_Converter_DoIt()函数:

  1. <?php
  2. define('DB_NAME', 'putyourdbnamehere');    // 数据库名
  3. define('DB_USER', 'usernamehere');     // MySQL用户名
  4. define('DB_PASSWORD', 'yourpasswordhere'); // 密码
  5. define('DB_HOST', 'localhost');    // 很大可能你无需修改此项
  6.  
  7. function UTF8_DB_Converter_DoIt() {
  8.     $tables = array();
  9.     $tables_with_fields = array();
  10.  
  11.     // Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
  12.     // we have to make an a stand-alone/direct connection to the database.
  13.     $link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
  14.     mysql_select_db(DB_NAME, $link_id);
  15.  
  16.     // Gathering information about tables and all the text/string fields that can be affected
  17.     // during the conversion to UTF-8.
  18.     $resource = mysql_query("SHOW TABLES", $link_id);
  19.     while ( $result = mysql_fetch_row($resource) )
  20.         $tables[] = $result[0];
  21.  
  22.     if ( !empty($tables) ) {
  23.         foreach ( (array) $tables as $table ) {
  24.             $resource = mysql_query("EXPLAIN $table", $link_id);
  25.             while ( $result = mysql_fetch_assoc($resource) ) {
  26.                 if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
  27.                     $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL "( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
  28.             }
  29.         }
  30.  
  31.         // Change all text/string fields of the tables to their corresponding binary text/string representations.
  32.         foreach ( (array) $tables as $table )
  33.             mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
  34.  
  35.         // Change database and tables to UTF-8 Character set.
  36.         mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
  37.         foreach ( (array) $tables as $table )
  38.             mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
  39.  
  40.         // Return all binary text/string fields previously changed to their original representations.
  41.         foreach ( (array) $tables_with_fields as $table => $fields ) {
  42.             foreach ( (array) $fields as $field_type => $field_options ) {
  43.                 mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
  44.             }
  45.         }
  46.  
  47.         // Optimize tables and finally close the mysql link.
  48.         foreach ( (array) $tables as $table )
  49.             mysql_query("OPTIMIZE TABLE $table", $link_id);
  50.         mysql_close($link_id);
  51.     } else {
  52.         die('<strong>There are no tables?</strong>');
  53.     }
  54.  
  55.     return true;
  56. }
  57. UTF8_DB_Converter_DoIt();
  58. ?>

现在你把上面代码保存到一个php文件中,上传至空间并运行,如无意外你的数据已经完成了latin1_swedish_ci到utf8_general_ci转换。

收藏到: 收藏到del.icio.us | 收藏到baidu搜藏 | 收藏到diglog | 收藏到饭否 | 收藏到Google | 收藏到新浪vivi | 收藏到mister wong | 收藏到挖客 | 收藏到qq书签

1留言于“latin1_swedish_ci to utf8_general_ci”

  1. g30rg3_x说:

    Hi, thats why i make this plugin open source…
    People can adapt the code to his own needs…

    But i think you should use the under development engine instead of the 2.0.1 engine, cause it will be prepare to handle the fulltext index problems…

    You can check it here
    UTF8_DB_Converter

    Greetings from mexico
    PS: At least one chinese word mmm, your name counts? :-P
    PS: The 1 one chinese word, hi 冰古

留言

提示/Tips:中国人使用中文!Your comment must include some Chinese in order to pass the comment checking.