0

I am using a PHP 5.2.17 server + MySQL 5.1.65.

I have a table containing a field that is VARCHAR utf8_general_ci and I fetch a record from this table.

This is how I open the connection, nothing special:

$link = mysql_connect('localhost', 'user', 'pass');

I need to respond with a JSON object that contain special characters Unicode escaped, I mean the \u00e1 notation.

 $result = mysql_query(sprintf("select * from data t where t.domain='%s'", escape($domain));


  while($row = mysql_fetch_array($result)) {
    $r[] = array(
      "tagid" => $row['DATAID'],
      "name" => $row['NAME']
    );
   )

$encoded = json_encode($r);
header('Content-type: application/json');
exit($encoded);

My problem is fields containing special characters (áé..) are returned as null in the JSON response.

After having Googled for a while I see that PHP 5.2 lack the json_encode parameters, so I need to unicode-escape name fields manually. But how could I do this?

1
  • any reason you can't upgrade your PHP 5.2? Seeing as it was declared end-of-life nearly two years ago already? Commented Oct 23, 2012 at 13:22

5 Answers 5

3

json_encode() should be able to handle UTF-8 data perfectly well - JSON is UTF-8 only, so it would be strange for PHP to not have this particular function as UTF-8 aware.

$array = array('key' => 'せ');

var_dump(json_encode($array)); // string(16) "{"key":"\u305b"}"

Your column collation might be VARCHAR utf8_general_ci but that does not mean the characters are encoded as UTF-8. Your table should be created with:

CREATE TABLE ... CHARACTER SET utf8

And you should probably execute this as your first query:

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

1 Comment

Thanks, all I was missing is the SET NAMES utf8 as a first query.
1

You can implement json_encode yourself, which should not be problematic, or check others' code, like this one (with optional tweaks when you find that necessary).

Comments

1

There's actually no need to do this escaping. What's much more likely, is that your input data is not actually valid UTF-8.

Changing the collation of a table may not fix this. Are you absolutely certain it's valid?

Also: stop using mysql_ functions and use PDO or mysqli. The mysql_ functions have been deprecated a long, long time ago.

Comments

0

You might also find that base64_encode(ing) the data will make json_encode more reliable. In your example: "tag

"tagid" => base64_encode($row['DATAID'])
...

Then you json_encode the whole thing. Of course you have to base64_decode at the other end.

Comments

0

If you (like me) are stuck with a pre 5.4 PHP environment, this works for latin accented multirow returns fetched using either mysql or mysqli (string originally MySQL utf8):

$encoded = jsonEncode($r);
exit($encoded);
function jsonEncode($outputArray) {
    $outputMe = "[";
    $k = 0;
    foreach ($outputArray as $key => $valueArray) {
        if ($k) { 
            $outputMe.=","; 
        }
        $outputMe.="{";
        $l=0;
        foreach ($valueArray as $position => $value) {
            if ($l) { 
                $outputMe.=","; 
            }
            $outputMe .= '"'.$position.'":"'.$value.'"';
            $l++;
        }
        $outputMe .= "}";
        $k++;
    }
    $outputMe .= "]";
    return $outputMe;
}

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.