0

i am using json output for my application and stored all data in my native language in mysql server with utf8_general_ci

when i am fetching that using json_encode i got the json array but the data format is not supported in that. how can i solve it.

code which i used to create json data.

<?php 
 header('Content-type: text/html; charset=utf-8; pageEncoding="ISO-8859-1"');
    include('include/config.php');

    mysql_query("SET NAMES 'utf-8' 'ISO-8859-1'");


    //mysql_query("SET CHARACTER SET utf8 ISO-8859-1");

    $sth = mysql_query("select v.verse,b.book_name,v.chapter,v.verse_number from tbl_verses_mal v  inner join tbl_books_mal b on v.book_id=b.book_id");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
}
print json_encode($rows);

?>

the output i got is like

[{"verse":"???????? ?????? ???????? ?????? ??? ????????? . ?? ????????? ??? ??????? ????????????????? .","book_name":"Genesis","chapter":"1","verse_number":"1"},{"verse":"???? ??????? ?????????? ??????? : ???????????? ???? ????????????? .????????????? ??????? ??????","book_name":"Genesis","chapter":"1","verse_number":"2"},{"verse":"???????? ?????????? ????? ???? ?????????: ???????? ??????? ","book_name":"Genesis","chapter":"1","verse_number":"3"}]

????? marks represents the language which is in the database. the expected results is like given below

[{"verse":"അയൽകാരന്  ആവശ്യം വരുമ്പോൾ നിങ്ങൾ  കടം കൊടുക്കു. .","book_name":"Genesis","chapter":"1","verse_number":"1"},{"verse":"ഭൂമി  പാഴായും ശൂന്യമായും  ഇരുന്നു : ആഴത്തിന്മീതെ","book_name":"Genesis","chapter":"1","verse_number":"2"},]

how can i solve this issue??

Usman
  • 3,200
  • 3
  • 28
  • 47
binil
  • 41
  • 1
  • 2
  • 6

2 Answers2

1
mysql_query("SET NAMES 'utf-8' 'ISO-8859-1'");

This makes no sense. Set the charset properly:

mysql_set_charset('utf8');
deceze
  • 510,633
  • 85
  • 743
  • 889
  • when set the charset mysql_set_charset('utf8');out put like [{"verse":"\u0d05\u0d2f\u0d7d\u0d15\u0d3e\u0d30\u0d28\u0d4d \u0d06\u0d35\u0d36\u0d4d\u0d2f\u0d02 \u0d35\u0d30\u0d41\u0d2e\u0d4d\u0d2a\u0d4b\u0d7e \u0d28\u0d3f\u0d19\u0d4d\u0d19\u0d7e \u0d15\u0d1f\u0d02 \u0d15\u0d4a\u0d1f\u0d41\u0d15\u0d4d\u0d15\u0d41\u0d15 . \u0d28\u0d40 \u0d35\u0d3e\u0d19\u0d4d\u0d19\u0d3f\u0d2f\u0d3e\u0d7d \u0d15\u0d1f\u0d02 \u0d38\u0d2e\u0d2f\u0d24\u0d4d\u0d24\u0d4d \u0d24\u0d3f\u0d30\u0d3f\u0d1a\u0d4d\u0d1a\u0d41\u0d15\u0d4a\u0d1f\u0d41\u0d15\u0d4d\u0d15\u0d41\u0d15 .","book_name":"Genesis","chapter":"1"}] – binil Jun 25 '13 at 05:44
  • And that's exactly what you want, it's JSON encoded Unicode characters. – deceze Jun 25 '13 at 05:47
  • i want the array in [{"verse":"അയൽകാരന് ആവശ്യം വരുമ്പോൾ നിങ്ങൾ കടം കൊടുക്കു. .","book_name":"Genesis","chapter":"1","verse_number":"1"},{"verse":"ഭൂമി പാഴായും ശൂന്യമായും ഇരുന്നു : ആഴത്തിന്മീതെ","book_name":"Genesis","chapter":"1","verse_number":"2"},] format. can i make it in this format – binil Jun 25 '13 at 05:50
  • That's the expected output. If you want to see the correct format you must use `$json['verse']`. – Dave Chen Jun 25 '13 at 05:54
  • 1
    @bini If you use PHP 5.4+ there's a flag you can set to use raw UTF-8; otherwise it's irrelevant what you want, this is how Unicode characters are encoded in normal JSON. – deceze Jun 25 '13 at 05:56
  • IMO: that's be harder to transfer and defeat the purpose of serialization. – Dave Chen Jun 25 '13 at 05:56
0

you need to set character set properly try like

mysql_set_charset('utf8');

And mysql_* function are deprecated use PDO or Mysqli instead

Community
  • 1
  • 1