2

阿拉伯字符的select语句返回?????.. 为什么?

php代码:

<?php

    require_once("db.php");
    $code=$_GET["code"];
    $query = "SELECT id,name,price,code from metro_goods where code=$code";

    $result = mysql_query($query) or die(mysql_error());

    $arr=array();
    $i=0;

    while ($row = @mysql_fetch_assoc($result)){
        $arr[$i]=array("id"=>$row['id'],"name"=>$row['name'],"price"=>$row["price"],"code"=>$row["code"]);
        $i++;
    }
        echo json_encode(array("data"=>$arr));
 ?>

结果 :

{"data":[{"id":"55121","name":"???? ??? ????? - ???","price":"16","code":"42217572"}]}

编辑:

设置字符集时

$rating_conn = mysql_connect($rating_dbhost, $rating_dbuser, $rating_dbpass) or die  ('Error connecting to mysql');
    mysql_set_charset('utf8',$rating_conn); 

输出变为:

{"data":[{"id":"55121","name":"\u0645\u0632\u064a\u0644 \u0639\u0631\u0642 \u0646\u064a\u0641\u064a\u0627 - \u0633\u062a\u0643","price":"16","code":"42217572"}]}
4

1 回答 1

1

在进行任何查询之前,您必须使用以下查询语句将字符集编码分配给 UTF8:

mysql_query("设置名称 utf8");

于 2013-07-06T11:41:15.370 回答