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.

4
  • Please use only prepared statements see stackoverflow.com/questions/60174/… Commented Mar 22, 2020 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? Commented Mar 22, 2020 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 Commented Mar 22, 2020 at 22:53
  • @nbk thanks for your guidance and help, I have applied prepared statement structure into the code but problem still persists. Commented Mar 23, 2020 at 9:15

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.