0

I have some persian data in my mysql database, the retrieved data contains C/C++/Java Source Code encoded data for persian values as below;

{"server_response":[{"hp_number":"09301234567","name":"\u0633"}]} while in name column there is a persian character (س).

the php code is as below:

<?php

require "init.php";

$hp_no = "09301234567";
mysqli_set_charset($con,"utf8");

$sql = "select * From users Where hp_number = ('$hp_no');";
$result = mysqli_query($con,$sql);

$response = array();

while ($row = mysqli_fetch_array($result)) {
  array_push($response,array("hp_number"=>$row[1],"name"=>$row[3]));
}

echo json_encode(array("server_response"=>$response));

mysqli_close($con);
?>

on mysql side encoding is set as utf8_general_ci for both table and database itself and is presented perfectly with no issue.

Thanks in advance for your helps and suggestions.

Trajox
  • 352
  • 3
  • 11
  • Please use only **prepared statements** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Mar 22 '20 at 22:40
  • @nbk since its just a test application i havent followed the prepared statements structure and I suspect that won't solve the issue, isn't it? – Trajox Mar 22 '20 at 22:48
  • yes, but even only test runs should be made secure, to be Prepared when it s going online. But you are correct, i can't see why you get this, the whole chain seems to be UTF8 even a switch to PDO wouldn't help. But as long there are no answers, why don't give it a try – nbk Mar 22 '20 at 22:53
  • @nbk thanks for your guidance and help, I have applied prepared statement structure into the code but problem still persists. – Trajox Mar 23 '20 at 09:15

1 Answers1

1

I could successfully solve the issue by adding JSON_UNESCAPED_UNICODE as another parameter while encoding. so the code line changes to;

echo json_encode(array("server_response"=>$response),JSON_UNESCAPED_UNICODE);

Trajox
  • 352
  • 3
  • 11