Abhilash Harsole
Abhilash Harsole

Reputation: 254

Display JSON data in Marathi language using PHP web service

I have a PHP web service in which I am retrieving data from mysql database. In database the data is stored in Marathi language, but when I am running my web service in postman, data is coming but many question mark symbol are displayed. How to solve this issue? Below is my PHP web service code:

<?php

//include config_db.php
include_once('config_db.php');

$qr = "SELECT * FROM `recipe_breakfast`";
$qrResult  = mysqli_query($db, $qr);
$count = mysqli_num_rows($qrResult);

if($count > 0)
{
    while($data = mysqli_fetch_assoc($qrResult)) 
    {
                $result[] = $data;
        }
    $response = array('msg' => 'Success','details' => $result);
}
else
{
    $response = array('msg' => 'No details found');
}

@mysqli_close($db);

/* Output header */
header('Content-type: application/json');
echo json_encode($response);

?>

Upvotes: 0

Views: 993

Answers (1)

Rei
Rei

Reputation: 6363

You have a charset issue.

In your code, after this line:

include_once('config_db.php');

Add this:

mysqli_query($db, 'set names utf8mb4');

If somehow your database doesn't support utf8mb4, use this instead:

mysqli_query($db, 'set names utf8');

This should solve the problem.

If it doesn't, you also have an issue in your table. The solution is to set the collation of every varchar or text column to utf8mb4_general_ci. This means that you have to redo the data entry.

Upvotes: 1

Related Questions