因为服务器提供商的原因只能通过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):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?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";
}
}
?>
</body>
</html>
显示的是乱码。
如果修改代码如下,并将文件的编码改为ANSI。则能正常显示
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?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";
}
}
?>
</body>
</html>
请问这是什么原因导致的乱码?如果要解决这个问题要怎么操作?