Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17912241
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-05-29 10:14:56

【解决php读取数据库乱码】

仍以数据库mysqlcode为例
CODE:
php 
$conn 
mysql_connect("localhost","root",""
); php程序员站
mysql_query("set names 'gbk'");
//这就是指定数据库字符集,一般放在连接数据库后面就系了
mysql_select_db("test"
);

$sql "select * from mysqlcode"
;
$result mysql_query($sql,$conn
);

?>
<
head
> php程序员站
<
meta http-equiv="Content-Type" content="text/html; charset=gb2312" 
/>
<
title>mysql 字符编码title>
head
>

<
body
>
<
table width="300" height="32" border="1" align="center" cellpadding="0" cellspacing="0"
php程序员之家

  <
tr
>
    <
td width="71" align="center">idtd>
    <
td width="229" align="center">内容td>
phperz.com

  tr
>
 php while($row mysql_fetch_assoc($result
)){
 echo 
"  
 
    "
.$row['id'].
"
    "
.$row['content'].
"
phperz.com

  "
;
}?>  
table
>
body
>
html
>
php mysql_free_result($result);?> 
如果我们将mysql_query("set names 'gbk'");注释掉,肯定时乱码
php程序员站

加上那句又正常了


一句话
你数据库用什么编码,在对数据库操作之前就set names '你的编码';

ps:页面申明编码:在HTML代码HEAD里面,可以用来告诉浏览器网页采用了什么编码,目前中文网站开发中主要用的是GB2312和UTF-8两种编码。
阅读(557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~