跳至主要内容

MySQL 4.1x 中文乱码问题

现在多数host装的还是MySQL 4.0x 版本以下,如果升级到了4.1x 或者移动到使用4.1x 的主机上,估计会遇到一些问题。我在powweb上遇到了两个问题。

一是数据导入。在phpMyAdmin内导入后,发现中文变成了乱码。搜索学习后,了解到需要设置数据库的collation (校勘),如果你的数据库是utf-8的,设置为utf8_general_ci就可以了。

二是,即使正确导入了,在phpMyAdmin里也能正确显示,但WordPress页面上显示的文章内容都是"???",而模板里面的汉字都能正常显示。这让我头疼了很久。搜索到Windix's Weblog上有对此的说明:

从MySQL 4.1开始引入的多语言支持确实很棒,而且一些特性已经超过了其他的数据库系统。不过我在测试过程中发现使用适用于MySQL 4.1之前的PHP语句操作MySQL数据库会造成乱码,即使是设置过了表字符集也是如此。我读了一下新的MySQL在线手册中第十章"Character Set Support "后终于找到了解决方法并测试通过。

MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。

当我们按照原来的方式通过PHP存取MySQL数据库时,就算设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:

SET NAMES 'utf8′;

但是我依旧不知道,如何,在何处让WordPress运行这个SQL语句。后来在阿修的部落格上查到办法:

為著解決這個問題必須去修改wp-includes/wp-db.php內的資料連線設定。詳細的修改方式是這樣的:

$this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
//加上下面這行
$this->query("SET NAMES 'utf8′");

这样子终于能正常显示中文的WordPress了。虽然后来还是没用Powweb,但是觉得这个经验会对别人有用。记得有人用email和我讨论过这样子的中文显示问题,但是我那时哪里知道问题出在MySQL 4.1x上。

还有,4.1x的数据库不向下兼容,所以升级的时候要三思啊。

评论

此博客中的热门博文

How to Design a Store Front Sign

In this Article we will talk about designing a store front for a business. What color or size should your Letters & designs be? What to put on your sign and why?, then be ready to Get in touch with sign people and get your best deal. Large lettering with out a front sign could save you time and money Do your products have a special shape or color to emulate on your sign? Kodak is always in yellow and black, Coca-Cola white on red, Etc. Muffler places, have a sign in the shape of a muffler. your phone number on the store front some place, but not on the identification sign, (door, or window) Your Product & logos can be arranged on the front sign, or window. Not on the Pole sign, Store hours & phone number could fit in your design and by using the product color, it could add product recognition & enhance your design. Suppliers may provide plenty of advert...

Can Microsoft and Yahoo Co-Create New Businesses?

Negotiating The Future of Management Podcast Current Interactive Case Study Respectfully there's a parallel in modern history here. China-HongKong merger. It started as Two System One China. Controversial at that time. Now, many don't mind which system is better (respectfully just an opinion). Both are getting the best of each other under one country. Families are reunited regularly. Corporate identities sometimes focuses too much on what's different as a strategy. It may provide superiority, but does it always guarantee sustainability? Nature advocates the flourishing of various species; pursues diversity not for purposes of destruction but for better adaptability. Of the whole system. Being different doesn't mean one must separate oneself from the "whole" system. "Different" here is to provide a unique contribution to pserve the "whole". Task / role distribution , not identity separation. Preserving the whole system by b...

MP3 firmware upgrade --- switch

1: The most simple if the host machine using Sigma firmware and upgrade program is one! MP3 can be connected to the computer as long as the firmware installed on the computer, and then click on the face of that to upgrade the line of MP3! three antelope 7530 master points in several ways is to first get the firmware is sent to the machine within the sub-sub-folder off the machine DVR will automatically upgrade! Detailed steps have been described in general firmware! Shenzhen City Qing Wei Digital Appliance Co., Ltd.-KINGWAY http://www.kingwon.com.cn/ Technology Co., Ltd. Shenzhen, Pu-chih http://www.poozi.com/down.asp?le ...% D4% D8 & offset = 5 Section Crystal: http://www.fangke-model.com/ Chuanqi (ONKI): http://www.onki.cn/download.asp brother extremely grateful! CDT CDT http://xinvo.com/web/ ExcelStor MP3 http://www.hua-sun.com/xiazai.htm Bester http://www.szbaisite.com Beto www.beto.cn Butterfly www.it8.com.cn Acorp www.acorp.net.cn BES www.bes-chi...