从旧主机搬到新主机最烦人的就是乱码问题。这也是个由来已久的问题了。
从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()函数:
<?php
define('DB_NAME', 'putyourdbnamehere'); // 数据库名
define('DB_USER', 'usernamehere'); // MySQL用户名
define('DB_PASSWORD', 'yourpasswordhere'); // 密码
define('DB_HOST', 'localhost'); // 很大可能你无需修改此项
function UTF8_DB_Converter_DoIt() {
$tables = array();
$tables_with_fields = array();
// Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
// we have to make an a stand-alone/direct connection to the database.
$link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
mysql_select_db(DB_NAME, $link_id);
// Gathering information about tables and all the text/string fields that can be affected
// during the conversion to UTF-8.
$resource = mysql_query("SHOW TABLES", $link_id);
while ( $result = mysql_fetch_row($resource) )
$tables[] = $result[0];
if ( !empty($tables) ) {
foreach ( (array) $tables as $table ) {
$resource = mysql_query("EXPLAIN $table", $link_id);
while ( $result = mysql_fetch_assoc($resource) ) {
if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
$tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " . ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
}
}
// Change all text/string fields of the tables to their corresponding binary text/string representations.
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
// Change database and tables to UTF-8 Character set.
mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
// Return all binary text/string fields previously changed to their original representations.
foreach ( (array) $tables_with_fields as $table => $fields ) {
foreach ( (array) $fields as $field_type => $field_options ) {
mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
}
}
// Optimize tables and finally close the mysql link.
foreach ( (array) $tables as $table )
mysql_query("OPTIMIZE TABLE $table", $link_id);
mysql_close($link_id);
} else {
die('<strong>There are no tables?</strong>');
}
return true;
}
UTF8_DB_Converter_DoIt();
?>
现在你把上面代码保存到一个php文件中,上传至空间并运行,如无意外你的数据已经完成了latin1_swedish_ci到utf8_general_ci转换。