| 
| 
 
| 
  
    | 
      
        | php读取mysql数据库乱码解决方法  |  
        | 
          
            |  |  
            | 来源:互联网  作者:未知  发布时间:2008-03-05 |  
            |  |  |  
        
          | 【解决php读取数据库乱码】
 
 仍以数据库mysqlcode为例
 
 如果我们将mysql_query("set names 'gbk'");注释掉,肯定时乱码<?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>  www~phperz~.com
 <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">id</td>
 <td width="229" align="center">内容</td>
 www phperz com </tr>
 <?php while($row = mysql_fetch_assoc($result)){
 echo "
 <tr>
 <td align=\"center\">".$row['id']."</td>
 <td>".$row['content']."</td>
 www.phperz.com </tr>";
 }?>
 </table>
 </body>
 </html>
 <?php mysql_free_result($result);?>
 
  PHP程序员站--PHP程序员之家 
 加上那句又正常了
 
  
 一句话
 你数据库用什么编码,在对数据库操作之前就set names '你的编码';
 
 ps:页面申明编码:在HTML代码HEAD里面,可以用<meta http-equiv="Content-Type" content="text/html; charset="XXX" />来告诉浏览器网页采用了什么编码,目前中文网站开发中主要用的是GB2312和UTF-8两种编码。
 
 |  
          |  |  
          | [收藏此页] [打印本页] [返回顶部] |  |  
    |  |  |   
|  |   
|  |  |  |  |  |