laZZySpiDer
laZZySpiDer

Reputation: 37

Return JSON Data in regional Language in PHP

I am using a MYSQL database where in I store the data in collation : utf16le_general_ci. I did this to support my regional language in the database and I am able store the data in my regional language(GUJARATI) successfully.

But when I use PHP(version 7+) to read the data and echo it in JSON format I get this kind of result :

[
    {
        "content_id":"1",
        "cat_id_FK":"1",
        "content_data":"??????????? ? ????? ??????? ??."
    },
    {
        "content_id":"2",
        "cat_id_FK":"1",
        "content_data":"???? ?? ??? ?? 80% ????? ???? ??? ? ??? ??, ???? ??? ?????? ?? ??????,?????????? ??? ?????\r\n??????? ?? ??? ??????? ???? ??? ??? ??."
    },
    {
        "content_id":"3",
        "cat_id_FK":"1",
        "content_data":"?????????? ??????? ????????? ???? ???? ???? ? ???????????."
    },
    {
        "content_id":"4",
        "cat_id_FK":"1",
        "content_data":"???????????  ??? ???? ?????? ?????? ???? ???? ????? ?? ???????? ?? ??? ??."
    },
    {
        "content_id":"5",
        "cat_id_FK":"1",
        "content_data":"??? ????? ????? ???????? ?? ????? ????? ??? ??????? ? ??????????? ?? ????? ???? ????."
    },
    {
        "content_id":"6",
        "cat_id_FK":"1",
        "content_data":"??????????? ???? ?? ??? ??? ??? ? ????? ???."
    }
]

My Code :-

header('Content-Type:application/json;charset=utf-8');
//header('Content-Type:application/json;charset=utf16le_general_ci');
include 'init.php';
global $connect;
$query = "SELECT * FROM gsrahasyacontent WHERE cat_id_FK=1";
$queryResult = mysqli_query($connect, $query);

while ($row = mysqli_fetch_assoc($queryResult)) {
   $array[] = $row;
}

echo json_encode($array, JSON_UNESCAPED_UNICODE);
//print json_encode($array, JSON_UNESCAPED_UNICODE);

-> I tried changing out the charset to utf16le_general_ci from utf-8 but that didn't worked.

-> I also used print instead of json but still got the same result.

This is what my table looks like.

Upvotes: 1

Views: 213

Answers (1)

Saurabh Sharma
Saurabh Sharma

Reputation: 440

try this mysqli_set_charset($con,"utf8");

Upvotes: 3

Related Questions