I have a PHP file which takes UTF-8 (Malayalam) words from a MySQL database and displays it in a browser after encoding it into JSON. The MySQL database is in UTF-8 format. The database contains Malayalam words. When I try to display the words without converting it into JSON, it displays fine as Malayalam, whereas when I convert it into JSON using json_encode
the Malayalam words are displayed as unknown characters, which I think is of ASCII format. I will show my PHP file and the code which I have used here:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$con=mysqli_connect("localhost","username","password","db_name");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con->set_charset("utf8");
$cresult = mysqli_query($con,"SELECT * FROM leaders");
$rows = array();
while($r = mysqli_fetch_assoc($cresult)) {
$rows[] = $r["name"];
//This displays the names correctly in malayalam like this: പോള് ജോസഫ്
// etc in the browser
//echo ($r["name"]);
}
$encoded= json_encode(array('Android' => $rows));
//Converting to json displays the names as weird characters like this:
// \u0d2a\u0d3f.\u0d35\u0d3f.\u0d2a\u0d4b\u0d33\u0d4d\u200d
echo ($encoded);
mysqli_close($con);
?>
</body>
</html>
How do I get Malayalam correctly as JSON? I need JSON because I need this JSON data sent to my client side (Android) for displaying it in my app. Please correct me if I'm going in the wrong track.