0

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.

4 Answers4

1
  1. Create your table [countrybasic] with collation utf8mb4_unicode_ci and make the name column with the same colation.
  2. Now insert some sample data in different languages.
  3. Get the data using MySQLi query result.

Note: If you save the data when the collation is different and get that one after changing the collation then that data will not fetch correctly.

I hope this will work.

0

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

MohammedAlSafwan
  • 872
  • 1
  • 8
  • 25
0
$CONNECTION = mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
   mysqli_query ($CONNECTION ,"set character_set_results='utf8'"); 
$queryutf8 = "select * from yourtable";
$res_utf8  = mysqli_query($CONNECTION ,$queryutf8 );
0

You need to change the default utf8 in the wamp server check the below link for more detail.

Arabic characters doesn't show in phpMyAdmin

Sandeep Sherpur
  • 2,418
  • 25
  • 27