phpandroidjsonutf-8urdu

Urdu / Arabic font data from MySQL is displaying as ????? in JSON


I am developing an Android app with Urdu/Arabic data store in MySQL database on my web server and using JSON_Encoding to generate the JSON string. The JSON string is then being used in Android app to perform various functions (populating RecyclerView and other view objects with data). I am able to store Urdu / Arabic data in MySQL database, but when I use PHP script to generate JSON, all the fields containing Urdu characters is displaying data as ??????

I was using the utf8mb4_unicode_ci as I read the this is easy for storing non-English data and performing multiple functions, but after this encoding problem, I have changed that to utf8_general_ci for all the tables and fields in MySQL database. Below is the PHP script I am using to generate the JSON string from MySQL:

<?php

require "conn.php";

mysqli_query("SET NAMES 'utf8'"); 
mysqli_query('SET CHARACTER SET utf8'); 

$sql_qry = "SELECT * FROM countrybasic;";

$result = mysqli_query($conn, $sql_qry);

$response = array();

while($row = mysqli_fetch_array($result)){
    array_push($response, array("id"=>$row[0],"name"=>$row[1],"capital"=>$row[2],"continent"=>$row[3],"population"=>$row[4],"gdp"=>$row[5]));
}

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

mysqli_close($conn);
?>

The Name and Capital fields are the ones I store my Urdu data in.

Please help me out to resolve this issue.

Thanks.


Solution

  • You just have to change the Charset to UTF8, and you can use these lines for PHP to do it:

    $statSQL= 'SET CHARACTER SET utf8'; 
    
    mysqli_query($your_db,$sSQL) 
    or die ('charset in DB didn\'t change'); 
    

    I hope this help :)