1

My php code is below :

<?php

$HostName = "localhost";
$HostUser = "root";
$HostPass = "pass";
$DatabaseName = "test";

$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$conn->set_charset("utf8");

if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM  demo";
$result = $conn->query($sql);
if ($result->num_rows >0) {

    while($row[] = $result->fetch_assoc()) {

       $tem = $row;
       $json = json_encode($tem);
    }
} else {
 echo "No Results Found.";
}
echo $json;
$conn->close();
?>

I expect the output of like this :

[{"id":"1","name":"ami","phone":"এক"},{"id":"2","name":"tmi","phone":"দুই"}]

but the actual output is

[{"id":"1","name":"ami","phone":"\u0987\u0982\u09b2\u09cd\u09af\u09be\u09a8\u09cd\u09a1"},{"id":"2","name":"tmi","phone":"\u0987\u0982\u09b2\u09cd\u09af\u09be\u09a8\u09cd\u09a1"}]
Dave
  • 5,108
  • 16
  • 30
  • 40
SM Mizan
  • 31
  • 7

1 Answers1

2

The reason you get this output is because the data is actually stored that way in the database. It's the json_encode method that converts it to this representation. Once you decode it with any json decoder, you should get your unicode characters back.

This is further explained in this answer: https://stackoverflow.com/a/594881/2232127

(To clarify: This is not a bug or a problem at all. It's intended and useful behavior which avoids encoding problems)

JensV
  • 3,997
  • 2
  • 19
  • 43