先将php 编码设为utf8:
header("Content-type: text/html; charset=utf-8");
从数据库取数据
$conn=mysql_connect("localhost","root","") or die("connot connect");
mysql_query("set names utf8");
mysql_select_db("database");
$sql="select * from table";
$re=mysql_query($sql,$conn);
将数据压入数组:
$arr=array();
while($row=mysql_fetch_array($re,MYSQL_ASSOC))
{
array_push($arr,$row);
}
编码输出json格式数据:
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
注:适合PHP5.4以上.