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?
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.Your column collation might be
VARCHAR utf8_general_cibut that does not mean the characters are encoded as UTF-8. Your table should be created with:And you should probably execute this as your first query: