首页 新闻 会员 周边

MySQL与网页显示乱码的问题

0
悬赏园豆:15 [已解决问题] 解决于 2013-12-17 23:27

各位对乱码解决方案熟悉的高手们,帮帮忙:

  情况是这样:(1)我的PHP文件已保存为utf-8格式,并且文档头信息声明了如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
</head>
大家应该都知道,此时文档中中文字符能正确显示。

(2)mysql配置时默认字符已经设置为utf8,phpmyadmin连接校对为:utf8_general_ci

(3)为了说明问题,本人写了下面小段代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf8">
</head>
<?php
mysql_connect("localhost","root","123")or die("can't connect");
mysql_select_db("db1");
if(isset($_GET["username"])) {
$sql = "insert into table1 values ('" . $_GET['username'] ."');";
mysql_query($sql) or die("false");
}
$sql = "select * from table1;";
$res = mysql_query($sql);
mysql_query("set names utf-8");
while($arr = mysql_fetch_assoc($res)) {
echo $arr['name'];
echo "<br />";
}

?>

<form method="get" ACTION='<?php "http://localhost" . $_SERVER[PHP_SELF]?>'>
<input type="text" name="username">
<input type="submit" name="submit" VALUE="提交">
</form>
</html>
这时问题来了,通过网页提交的数据可以在网页中正常显示,但在phpmyadmin里面浏览表时显示乱码;而通过phpmyadmin插入table1中的数据在phpmyadmin中正确显示但在网页中显示乱码!

  请大家帮忙,感激不尽!

Vincent的柔情的主页 Vincent的柔情 | 初学一级 | 园豆:27
提问于:2010-12-01 22:33
< >
分享
最佳答案
0

请问你装数据库时候是UTF8的,还是GB版本的。要检查清楚。

我之前这个网站www.krzhjiaju.com也出现过类似的问题,后来调试了下。就正常了。

收获园豆:15
有客来 | 菜鸟二级 |园豆:245 | 2010-12-13 20:30
其他回答(1)
0

mysql_query("set names utf-8");
这句话放到mysql_select_db("db1");
后面。放到后面无效

郎玛传说 | 园豆:205 (菜鸟二级) | 2011-11-17 17:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册