2

My php web service uses json_encode to return json data fetched from MySQL database table. The data should be returned in whatever local language as unicode. Instead local characters like hindi/telugu/bengali are displayed as html entities. But output is needed as unicode and NOT html entities.

<?php

    //header('Content-Type: text/html;charset: utf8'); //wasted code line

    //Connection and credentials
    $servername = "xxx.yyy.zzz.nnn";
    $username = "username";
    $password = "password";
    $dbname = "mydb";

    //variables to store fetched data
    $item[]=array();
    $dataArray[] = array();

    // Create connection
    $conn = mysql_connect($servername, $username, $password);

   //mysql_set_charset('utf8', $conn); //wasted code line

   $mytopid = mysql_real_escape_string($_GET['mytopid']); //get input
   $query = "SELECT  * FROM datamaster where Id > '$mytopid' order by Id desc"; //Now query 

   //mysql_query("set names 'utf8'"); //wasted code line

    if ($result=mysql_query($query,$conn)){   
        $rows = mysql_numrows($result);
        $i= 0;
        while ($i < $rows){    
           //fetch data  
           $row = mysql_fetch_array($result);

           //wasted code lines
           //$dataArray[$i]["shortid"] = utf8_encode($row['Id']);
           //$dataArray[$i]["shorttitle"] = utf8_encode($row['Title']);

           //reality
           $dataArray[$i]["shortid"] = $row['Id'];
           $dataArray[$i]["shorttitle"] = $row['Title'];
           $i++;
        }
        $item[0]["response"] = 1; 
        $item[1]["matching"] = $rows; 
        $item[2]["events"]=$dataArray;
        echo json_encode($item);
     }else{
        $item[0]["response"] = 0;
    }
    //echo json_encode($item, JSON_HEX_TAG| JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); //wasted code line

    mysql_close($conn);

?>

Got Output: Actual Output

Required Output: [{"shortid":"5","shorttitle":"\u0c38\u0c32\u0c4d\u0c2e\u0c3e\u0c28\u0c4d\u200c \u0c05\u0c21\u0c3f\u0c17\u0c3f\u0c28\u0c3e \u0c05\u0c35\u0c15\u0c3e\u0c36\u0c2e\u0c3f\u0c35\u0c4d\u0c35\u0c32\u0c47\u0c26\u0c41!"}]

I finally convert it at my client program decode to local language. The required output is supposed to be the default behaviour of json_encode. Despite most trials per php documentation (see the commented CODE lines that show my trials //wasted code line) the output continues to be in html entities except for English language.

My client programming language does not translate html entities.

Is there a php way to get the required output? I have tried every possible concept on stack overflow and in php documentation. Now I need help.

Please refer to my database language settings

1
  • Does the data in your database already contain HTML entities? Commented Oct 19, 2016 at 0:28

3 Answers 3

4

Use json_encode($item, JSON_UNESCAPED_UNICODE);. It will encode multibyte characters literally. You can get more info here.

Actually it's strange your client side cannot handle escaped characters. What do you use exactly?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Wazelin for the prompt reply. I am able to temporarily use html_entity_decode. Not a real solution though. Please refer to the answer I had posted a minute ago and please comment.
On the client side I am using Swift 3 for iOS devices.
0

Thnak you Wazelin for the prompt reply. I had tried the JSON_UNESCAPED_UNICODE approach too earlier. But that output continued to be html entities.

A temporary fix to the problem I have incidentally tumbled in to using html_entity_decode.

I changed the code lines in while loop to:

     while ($i < $rows){    
       //fetch data  
       $row = mysql_fetch_array($result);

       //reality
       $dataArray[$i]["shortid"] = $row['Id'];

       //handle html entities of local language
       $dataArray[$i]["shorttitle"] = html_entity_decode($row['Title']);
       $i++;
    }

That had given the required output. But doing this to every single web service is not like an ideal solution. Quite strangely the default json_encode behaviour is very elusive in my case.

2 Comments

Here is a stackoverflow link I came across that helped my temporary solution: stackoverflow.com/questions/14073009/…
Are you sure that data stored in your DB is not already escaped?
0

Check documentation: http://php.net/manual/en/function.json-encode.php

There are some options which allows you to encode your data correctly.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.

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.