json_encode() doesn't display arabic character

2019-02-21 18:39发布

This question already has an answer here:

i have problem with Arabic characters when i do json_encode() it always return ????, in the database all the fields and database is utf8

my code:

$query   = mysql_query("SELECT * FROM `Names`");

if (!$query) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
die($message);
}else
{
    while ($row = mysql_fetch_assoc($query)) 
    {
     $result[] = array(
        'Mid' => $row['Mid'], 
        'Uid' => $row['Uid'], 
        'Cid' => $row['Cid'], 
        'Name' => $row['Name'],
        'city' => $row['city'],
        'status' => $row['status'],
        'Mobile' => $row['Mobile'],
        'Phone' => $row['Phone'],
        'Email' => $row['Email']);
    }
      header('Content-Type: application/json; charset=utf-8');
      echo json_encode($result);
}

the result look like:

[{"Mid":"17","Uid":"1","Cid":"8","Name":"???? ?? ??????? ?? ???","city":"?????",

please help me

标签: php json arabic
1条回答
Summer. ? 凉城
2楼-- · 2019-02-21 19:09

Try this before sending your query

mysql_query("SET NAMES 'utf8'");

or this (if your PHP version is 5.4.0 +)

json_encode($result, JSON_UNESCAPED_UNICODE);

Note: In case that your data are stored in hex format, enclose json_econde with mysql_escape_string()

查看更多
登录 后发表回答