1

I am writing an android app and i want to query a db and get the results with json_encode. So i have this db:

CREATE DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

I use utf8 because i want to store greek characters. All the tables have utf8_general_ci collation.

At last i have this php service that i use to query my db.

<?php
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "my_db";

$con = mysql_connect($host,$username,$password) or die ('Error connecting to mysql');

mysql_query("SET character_set_results=utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);
mysql_select_db($dbname,$con);

$query = "SELECT * from patients";
$result = mysql_query($query,$con);
while($e=mysql_fetch_assoc($result))
        $output[]=$e;

print(json_encode($output));
mysql_close($con);
?>

The problem is that in the output i dont get greek characters. Instead i get this:

[{"patient_id":"2","email":"patienta@mail.com","password":"password2","firstname":"\u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3","lastname":"\u039c\u03a0\u0391\u0396\u0399\u03a9\u03a4\u0397\u03a3","birth":"1989-11-11","mobile":"6941212121","tameio":"\u039f\u0393\u0391"}]
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80
  • 1
    If you use json decode in client (like JSON.parse in JavaScript or json_decode in php) it will decode unicode escape characters. – jcubic Jul 21 '13 at 13:15
  • possible duplicate of [php-json output web service problem with utf-8 characters (greek)](http://stackoverflow.com/questions/6397534/php-json-output-web-service-problem-with-utf-8-characters-greek). You might also want to take a look at my answer to [Degree '°' character not displaying in php json\_encode function, how to display this?](http://stackoverflow.com/questions/15964967/degree-character-not-displaying-in-php-json-encode-function-how-to-display) – PleaseStand Jul 21 '13 at 13:17

1 Answers1

1

That is right. All the chars not in ascii will be converted into UTF-8 format.

The data will be decoded correctly by json_decode.

srain
  • 8,944
  • 6
  • 30
  • 42
  • I didnt know that. I did the same thing but with english characters, and the result wasn't encoded like this. Thanks i was trying for an hour to find out what i was doing wrong. – Christos Baziotis Jul 21 '13 at 13:21
  • The data will be decoded correctly by `json_decode`. Do worry about that. – srain Jul 21 '13 at 13:24