php页面添加了header()之后,echo输出的汉字就乱码,数据库查询的汉字正常显示。

<?php
header("Content-type: text/html; charset=utf-8");
$link=mysql_connect('localhost','root','');
if(!$link){
echo "连接失败<br>";
exit;
}echo "连接成功<br>";
mysql_select_db('dbphp',$link);
$sql="select id,name from db_shops";
$result=mysql_query($sql);
while($data=mysql_fetch_row($result)){
print_r($data);
echo "<br>";
}
mysql_close();
为什么添加了header()之后,echo输出的汉字就乱码,数据库查询的汉字正常显示。去掉header()之后,echo 输出正常,数据库的乱码?
哪位高手帮解答一下!!!

你在header里面设置了你文件的编码是uft-8,如果乱码,你要检查文件的编码和数据库的编码是不是也是utf-8的。追问

数据库编码都是utf8的,文件编码是指的哪个?

追答

header里面设置了你文件内容的编码是uft-8,文件编码指的就是你的那个conn.php,你用记事本打开它,另存为,就会看到最底下有个编码,应该为utf-8,如果是ANSI就表示是中文编码。

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-05-16
在最上面输入
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
mysql_select_db('dbphp',$link); 在这个后面
mysql_query(set names utf8);
相似回答