您的位置:首页 > 编程语言 > PHP开发

phpmyadmin显示mysql 中文乱码问题

2008-09-27 16:14 375 查看
编辑网页碰到中文乱码是件很头痛的事情,牵扯到网页的编码。
今天用phpmyadmin向mysql录入中文数据的时候又碰到这类麻烦问题了。写好的php页面从mysql读取出来的数据全成了火星文。老规矩网上google一下,弄了一天终于解决问题了。网上出现同类问题的兄弟很多,现在把我总结的方法拿出来分享
关于为什么会出现乱码台湾的 http://www.adsenseor.com/mysql/256.html 讲的很详细。我就跳过很多废话,直奔问题所在了。
mysql字符集默认的是latin1,通过phpmyadmin录入的utf8编码的数据都会以latin1的编码方式进行再编码,而破坏了原来的数据编码,所以无论你将浏览器的页面字符编码更改成哪种方式都还是乱码。
更改mysql的默认编码方式,可以用命令行进行改变。而我选择通过改变 mysql 全局变量 /etc/my.cnf (linux系统,windows环境 找my.ini),在Unix/inux平台上,MySQL忽略人人可写的配置文件。这是故意的,是一个安全措施。
mysql安装完成后(我是源码编译安装的),在/mysql/surrport-script的目录下有my-small.cnf等。分别是:

my-small.cnf:

1 # Example MySQL config file for small systems.

2 #

3 # This is for a system with little memory (<= 64M) where MySQL is only used

4 # from time to time and it's important that the mysqld daemon

5 # doesn't use much resources.

my-medium.cnf:

1 # Example MySQL config file for medium systems.

2 #

3 # This is for a system with little memory (32M - 64M) where MySQL plays

4 # an important part, or systems up to 128M where MySQL is used together with

5 # other programs (such as a web server)

my-large.cnf:

1 # Example MySQL config file for large systems.

2 #

3 # This is for a large system with memory = 512M where the system runs mainly

4 # MySQL.

my-huge.cnf:

1 # Example MySQL config file for very large systems.

2 #

3 # This is for a large system with memory of 1G-2G where the system runs mainly

4 # MySQL.

my-innodb-heavy-4G.cnf:

1 #BEGIN CONFIG INFO

2 #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries

3 #TYPE: SYSTEM

4 #END CONFIG INFO

5

6 #

7 # This is a MySQL example config file for systems with 4GB of memory

8 # running mostly MySQL using InnoDB only tables and performing complex

9 # queries with few connections.
选择一个合适你所在环境的脚本,我选的是meidum :sudo cp my-meidum.cnf /etc/my.cnf
打开找到一下命令
[mysql]

default-character-set=latin1

[mysqld]

default-character-set=latin1

将latin1改为 utf8 (如果没有就自己加上);
更改默认字符集后,还得把phpmyadmin的连接校对方式全部改成你所选择的字符集,这里是 utf8_general_ci。
注意:不但数据库的连接校对方式要改,表单的连接校对方式也得改

好了,现在通过phpmyadmin 录入的数据都可以正常显示了。但是php做的页面还会显示乱码。得在连接数据库的代码中添加指定字符集,你所选择的数据库的字符集是什么,你就在后面加上该种字符集就行了。例如
<?php

$conn = mysql_connect("localhost","root","");

mysql_query("set names 'utf8'");//这就是指定数据库字符集,一般放在连接数据库后面就系了

mysql_select_db("test");


?>
ok,乱码问题解决了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐