Home > Database > Mysql Tutorial > body text

MySQL从latin1编码向utf-8转换的经验分享

WBOY
Release: 2016-06-07 16:52:30
Original
837 people have browsed it

许多人可能会有这样的经验:明明全部系统都以UTF-8设置,但是用phpMyAdmin去看资料库时,发现中文字仍然是乱码。 若有发生这种情

许多人可能会有这样的经验:明明全部系统都以UTF-8设置,但是用phpMyAdmin去看资料库时,发现中文字仍然是乱码。

若有发生这种情况,是因为虽然mysql本身及网页都是utf-8,但是xoops却用latin1将数据传给mysql,因此最后是utf-8数据被以latin1的格式储存进资料库。其实在xoops来看没有任何问题,只是用phpMyAdmin看时,就都是乱码,较不好管理。

以下是环境:
mysql: 5.1.22
mysql文字编码: utf-8 unicode
mysql连线校对: utf-8_unicode_ci
所有资料库都以utf-8建立。
phpMyAdmin: 2.11.3 (Language选「中文- Chinese Traditional」)
xoops 2.0.17.1

对于这种状况,解决的几个面向:

1. mysql设定:
/etc/my.cnf (或Windows下的my.ini)要有以下设定:

[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
init_connect='SET NAMES utf8'

2.在xoops端,先前已有各位前辈在告知大家要在
xoops/class/database/mysqldatabase.php加入以下这行:
@mysql_query("SET NAMES 'utf8'", $this->conn);
(加此行在$result = mysql_query($sql, $this->conn);之前)
就可以强制用utf8传输。

3. mySQL资料库:
可是若有旧的database怎么办?照上面的方法改了之后,迈入xoops后以前所有的中文字都变乱码了,新输入或重新输入的中文字才会变正常显示。

没关系,照以下的步骤,,就能将以前的资料库转码了:

先采以下方法将资料库dump出:
mysqldump --all-databases --default-character-set=latin1 -u root -p > dump.sql

修改dump.sql文件首页的:
/*!40101 SET NAMES latin1 */;
成为
/*!40101 SET NAMES utf8 */;

然后再将资料库import回即可:
mysql -u root -p

重新打开xoops看,中文一切OK。进入phpMyAdmin看,也都没有乱码了。

Have a nice day!

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!