各位对乱码解决方案熟悉的高手们,帮帮忙:
情况是这样:(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>
请大家帮忙,感激不尽!
mysql_query("set names utf-8");
这句话放到mysql_select_db("db1");
后面。放到后面无效