Home > Backend Development > PHP Tutorial > 求教php+mysql乱码的问题

求教php+mysql乱码的问题

WBOY
Release: 2016-06-06 20:52:07
Original
804 people have browsed it

因为服务器提供商的原因只能通过phpMyadmin管理数据库。使用show variables语句查询得到的当前数据库的信息如下:
character_set_client latin1
character_set_connection utf8
character_set_database utf8
character_set_results latin1
character_set_server latin1
character_set_system utf8
collation_connection utf8_unicode_ci
collation_database utf8_unicode_ci
collation_server latin1_swedish_ci

我写了一个测试文件mytest.php代码如下(文件编码为UTF-8):


	
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title></title>
	

<?php print "Hello";
	$connection = mysql_connect('host','dbname','dbpass');
	mysql_select_db('obbdb', $connection);
	mysql_query("SET NAMES 'utf8'", $connection);
	mysql_query("SET CHARACTER SET utf8", $connection);
	mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $connection);
	mysql_query("SET SQL_MODE = ''", $connection);
	$result = mysql_query ("SELECT * FROM navigator", $connection);
	while ($row = mysql_fetch_array($result, 10)) 
	{
		foreach ($row as $attribute) {
			print "{$attribute}";
			print "\n";
		}
	}
?>

Copy after login
Copy after login

显示的是乱码。
如果修改代码如下,并将文件的编码改为ANSI。则能正常显示


	
		<title></title>
	

<?php print "Hello";
	$connection = mysql_connect('host','dbname','dbpass');
	mysql_select_db('obbdb', $connection);
//	mysql_query("SET NAMES 'utf8'", $connection);
//	mysql_query("SET CHARACTER SET utf8", $connection);
//	mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $connection);
	mysql_query("SET SQL_MODE = ''", $connection);
	$result = mysql_query ("SELECT * FROM navigator", $connection);
	while ($row = mysql_fetch_array($result, 10)) 
	{
		foreach ($row as $attribute) {
			print "{$attribute}";
			print "\n";
		}
	}
?>

Copy after login
Copy after login

请问这是什么原因导致的乱码?如果要解决这个问题要怎么操作?

回复内容:

因为服务器提供商的原因只能通过phpMyadmin管理数据库。使用show variables语句查询得到的当前数据库的信息如下:
character_set_client latin1
character_set_connection utf8
character_set_database utf8
character_set_results latin1
character_set_server latin1
character_set_system utf8
collation_connection utf8_unicode_ci
collation_database utf8_unicode_ci
collation_server latin1_swedish_ci

我写了一个测试文件mytest.php代码如下(文件编码为UTF-8):


	
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title></title>
	

<?php print "Hello";
	$connection = mysql_connect('host','dbname','dbpass');
	mysql_select_db('obbdb', $connection);
	mysql_query("SET NAMES 'utf8'", $connection);
	mysql_query("SET CHARACTER SET utf8", $connection);
	mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $connection);
	mysql_query("SET SQL_MODE = ''", $connection);
	$result = mysql_query ("SELECT * FROM navigator", $connection);
	while ($row = mysql_fetch_array($result, 10)) 
	{
		foreach ($row as $attribute) {
			print "{$attribute}";
			print "\n";
		}
	}
?>

Copy after login
Copy after login

显示的是乱码。
如果修改代码如下,并将文件的编码改为ANSI。则能正常显示


	
		<title></title>
	

<?php print "Hello";
	$connection = mysql_connect('host','dbname','dbpass');
	mysql_select_db('obbdb', $connection);
//	mysql_query("SET NAMES 'utf8'", $connection);
//	mysql_query("SET CHARACTER SET utf8", $connection);
//	mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $connection);
	mysql_query("SET SQL_MODE = ''", $connection);
	$result = mysql_query ("SELECT * FROM navigator", $connection);
	while ($row = mysql_fetch_array($result, 10)) 
	{
		foreach ($row as $attribute) {
			print "{$attribute}";
			print "\n";
		}
	}
?>

Copy after login
Copy after login

请问这是什么原因导致的乱码?如果要解决这个问题要怎么操作?

mysql_query("set names utf8");
放在链接数据库之后 所有查询语句之前
然后检查下所有文件是否都是 utf8编码
最后 将 html 代码的meta 信息 增加 utf8 的处理 应该可以解决

统一编码吧。mysql全用utf8,php文件只要是utf8就行了,不要另外设置编码。

解决办法:

3.修改my.ini,在[client]、[mysqld]、[mysql]后面加default-character-set = utf8,在[mysqld]下加,basedir、datadir。
[client]
default-character-set = utf8
[mysqld]
default-character-set = utf8
default-collation=utf8_bin
init_connect='SET NAMES utf8'
basedir=D:\mysql\
datadir=D:\mysql\data\
[mysql]
default-character-set=utf8

参考:http://hi.baidu.com/hiran/blog/item/5...

补充:这服务商也够变态的,数据库都utf8了,连接时也是utf8,返回的结果却用latin1。。。你的原因就出来这。如果不能更改服务器配置,你把返回的结果用iconv转成utf8试试应该就好了。

html的

标签里加上试试。
Related labels:
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