2

I am getting data from MySQL and displaying in json format using php but my data are in Persian language and it show ? character instead of the original data like given below.

{"allnews":[{"id":"35","Onvan":"???? ???? ????? ????? ?????? ?? ??? ?????"},{"id":"36","Onvan":"?????????? ???? ? ??????????? ?????"},{"id":"37","Onvan":" ??????? ???? ??? ??? ???? ???? ?? ??? ?? ???? ???? ??????? ??? ?????? ???????? ?? "},{"id":"38","Onvan":" ????? ????????? ???? ??? ?? ?? ??????? ????????"},{"id":"39","Onvan":"??? ???? ??? ????"}]}

Here is my code .Can you please check where has it gone wrong.

<?php
$hostname='localhost';
$username='xxxxxxxxx';
$password='xxxxxxxxx';
$response = array();
try {

    $dbh=new PDO("mysql:host=$hostname;dbname=dbtest",$username  ,$password);

    $response["allnews"] = array();

    /*** QUERY ****/
    $sql='SELECT * FROM test';

    $stmt=$dbh->query($sql);

    $objs = $stmt->fetchAll(PDO::FETCH_OBJ);

    foreach($objs as $object) {
        $news = array();        
        $news["id"]=$object->id;
        $news["Onvan"]=$object->title;

        array_push($response["allnews"], $news);
    }

    echo json_encode($response);

    /*** close connection ***/
    $dbh=null;

}catch(PDOException $e) {
 echo $e->getMessage();
}
?>
2
  • 1
    I have to ask, are you sure they are stored correctly in the database? Commented Jul 7, 2015 at 5:26
  • yes,ekhteraat.com/app/db.png Commented Jul 7, 2015 at 6:13

3 Answers 3

3

Use

header("Content-type: application/json; charset=utf-8");

Just before

echo json_encode($response);

And It also may be Mysql Fetching Error

So replace line

$dbh=new PDO("mysql:host=$hostname;dbname=dbtest",$username  ,$password);

with

$dbh=new PDO("mysql:host=$hostname;dbname=dbtest;charset=utf8",$username  ,$password);
Sign up to request clarification or add additional context in comments.

5 Comments

do print_r($response) before json_encode(), if it is not readable then it's mysql fetching problem.
Returns --> Ø«Ø±ÙØª Ú©ÙØ§Ù ÙØ®ØªØ±Ø¹ Ø§Ø³Ù¾Ø±Û Ø¯Ø§ÙØ±Ø§Ù در Ø¬Ø§Ù Ø¬ÙØ§ÙÛ
replace line $dbh=new PDO("mysql:host=$hostname;dbname=dbtest",$username ,$password); with $dbh=new PDO("mysql:host=$hostname;dbname=dbtest;charset=utf8",$username ,$password);
i replace but not work,Returns --> Ø«Ø±ÙØª Ú©ÙØ§Ù ÙØ®ØªØ±Ø¹ Ø§Ø³Ù¾Ø±Û Ø¯Ø§ÙØ±Ø§Ù در Ø¬Ø§Ù Ø¬ÙØ§ÙÛ
my corect data in db -> ثروت کلان مخترع اسپری داوران در جام جهانی | print_r returns - > Ø«Ø±ÙØª Ú©ÙØ§Ù ÙØ®ØªØ±Ø¹ Ø§Ø³Ù¾Ø±Û Ø¯Ø§ÙØ±Ø§Ù در Ø¬Ø§Ù Ø¬ÙØ§ÙÛ | json_encode retern -> \u00d8\u00ab\u00d8\u00b1\u00d9\u0088\u00d8\u00aa \u00da\u00a9\u00d9\u0084\u00d8\u00a7\u00d9\u0086 \u00d9\u0085\u00d8\u00ae\u00d8\u00aa\u00d8\u00b1\u00d8\u00b9 \u00d8\u00a7\u00d8\u00b3\u00d9\u00be\u00d8\u00b1\u00db\u008c \u00d8\u00af\u00d8\u00a7\u00d9\u0088\u00d8\u00b1\u00d8\u00a7\u00d9\u0086 \u00d8\u00af\u00d8\u00b1 \u00d8\u00ac\u00d8 ....
2

Best and simple solution

$dbh=new PDO("mysql:host=$hostname;dbname=dbtest",$username  ,$password);

add charset to above line like ( mysql:charset=utf8mb4; )

$dbh=new PDO("mysql:charset=utf8mb4;host=$hostname;dbname=dbtest",$username  ,$password);

Comments

1

You can use utf8_encode(String)

foreach($objs as $object) {
        $news = array();        
        $news["id"]=utf8_encode( $object->id);
        $news["Onvan"]=utf8_encode( $object->title);

        array_push($response["allnews"], $news);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.