php数据数据乱码问题

一 介绍

1、如果建立数据库时,使用的是utf8_unicode_ci,则在php程序中需要加mysql_query("set names utf8");
2、如果建立数据库时,使用的是GBK或GB2312,则在php程序中需要加mysql_query("set names gbk");  或  mysql_query("set names gb2312");
二 代码
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>	
<?php
$host = "127.0.0.1";
$userName = "root";
$password = "root";
$dbName = "db_test01";
$connID = mysql_connect($host, $userName, $password) or die("数据库连接失败!");
mysql_select_db($dbName, $connID);
mysql_query("set names utf8");
$sql = mysql_query('select * from tb_info');
while($result = mysql_fetch_array($sql))
{
    echo $result['title'];
}
?>
</body>
</html>
 
三 运行结果
从基础到实战

猜你喜欢

转载自cakin24.iteye.com/blog/2378250
今日推荐