Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode() doesn't display arabic characters in good way [duplicate]

Tags:

json

php

arabic

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

like image 559
Sideeq Youssef Avatar asked Jul 20 '14 15:07

Sideeq Youssef


People also ask

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

What does json_encode return?

Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What is JSON encode decode?

Source code: Lib/json/__init__.py. JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript 1 ).


1 Answers

Try this before sending your query

mysql_query("SET NAMES 'utf8'");

or this (if your PHP version is 5.4.0 or above)

json_encode($result, JSON_UNESCAPED_UNICODE);

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

like image 156
hex494D49 Avatar answered Sep 19 '22 21:09

hex494D49