日期:2014-05-16  浏览次数:20702 次

PHP连MYSQL输出中文乱码

我租用的虚拟主机,现在的服务器信息如下:
MY SQL服务器: 版本为5.0.22, Linux
PHP网站空间: win2k

我连之前的一个4.0版本的MY SQL(基于WIN)没有问题. 后来把数据库导到现在的基于LINUX的MY SQL服务器后,在PHPMYADMIN下查询数据库中文一切正常, 但在网站页面中输出中文却是乱码. 搞了一天不知道什么原因, 试了网上很多方法也没成功. 下面是一些字符集信息, 请大家帮我看看是什么原因. 谢谢!

在PHP程序中已经使用了mysql_query( "SET NAMES 'utf8'" );

mysql>SHOW CREATE TABLE my_menu
====================
CREATE TABLE `my_menu` (\n `menuid` int(12) NOT NULL auto_increment,\n `pid` int(6) default NULL,\n `menu` varchar(50) collate utf8_unicode_ci default NULL,\n `coltype` varchar(30) collate utf8_unicode_ci NOT NULL default '',\n `url` varchar(200) collate utf8_unicode_ci default NULL,\n `fold` varchar(50) collate utf8_unicode_ci NOT NULL default '',\n `pub` varchar(50) collate utf8_unicode_ci default NULL,\n `ifshow` int(1) default NULL,\n `moveable` int(1) default NULL,\n `xuhao` int(4) default NULL,\n `target` varchar(10) collate utf8_unicode_ci default NULL,\n `openindex` int(1) NOT NULL default '0',\n `secure` int(1) NOT NULL default '0',\n `skin` varchar(20) collate utf8_unicode_ci NOT NULL default 'images',\n PRIMARY KEY (`menuid`)\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


mysql>SHOW FULL COLUMNS FROM my_menu;
=============================
Field Type Collation Null Key Default Extra Privileges Comment  
menuid int(12) NULL NO PRI NULL auto_increment select,insert,update,references  
pid int(6) NULL YES NULL select,insert,update,references  
menu varchar(50) utf8_unicode_ci YES NULL select,insert,update,references  
coltype varchar(30) utf8_unicode_ci NO select,insert,update,references  
url varchar(200) utf8_unicode_ci YES NULL select,insert,update,references  
fold varchar(50) utf8_unicode_ci NO select,insert,update,references  
pub varchar(50) utf8_unicode_ci YES NULL select,insert,update,references  
ifshow int(1) NULL YES NULL select,insert,update,references  
moveable int(1) NULL YES NULL select,insert,update,references  
xuhao int(4) NULL YES NULL select,insert,update,references  
target varchar(10) utf8_unicode_ci YES NULL select,insert,update,references  
openindex int(1) NULL NO 0 select,insert,update,references  
secure int(1) NULL NO 0 select,insert,update,references  
skin varchar(20) utf8_unicode_ci NO images select,insert,update,references  


mysql>SHOW VARIABLES LIKE 'char%'
===============================
Variable_name Value  
character_set_client latin1 
character_set_connection utf8 
character_set_database utf8 
character_set_filesystem binary 
character_set_results latin1 
character_set_server latin1 
character_set_system utf8 
character_sets_dir /usr/share/mysql/charsets/ 



------解决方案--------------------
mysql>SHOW VARIABLES LIKE 'char%'
===============================
Variable_name Value 
character_set_client latin1
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results latin1
character_set_server latin1
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/


直接在MYSQL命令行工具中或者MYSQL BROWSER图形工具先确认一下你的字段中存储的是UTF8编码。