选择阿拉伯字符会导致MYSQL中出现问号。怎么修?

时间:2012-08-02 17:04:17

标签: php mysql select encoding arabic

阿拉伯字符的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"}]}

编辑:

设置char-set

$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"}]}

1 个答案:

答案 0 :(得分:1)

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

mysql_query(&#34; SET NAMES utf8&#34;);