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
The json_encode() function is used to encode a value to JSON format.
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.
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 ).
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With