0

I have some data in the database that contains characters such as ä, å, and ö, and when I try to get the data with json, it returns null instead of the actual data.

Any idea how to fix this problem?

Here is the code I am using:

    $result=  mysql_query('SELECT * FROM Recipes ORDER BY `ID` DESC LIMIT 5');
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
       $out[]=$row;
    }

    print(json_encode($out));

I tried everything I came across when googling, yet didn't solve the problem.

NorthernLights
  • 370
  • 3
  • 16
  • http://php.net/manual/en/function.json-last-error.php http://php.net/manual/en/function.json-last-error-msg.php – Sammitch May 08 '15 at 17:41
  • i got the following error: Unexpected character in input: '\' (ASCII=92) state=1 – NorthernLights May 08 '15 at 17:52
  • No actually the problem is that you need to give a utf8 encoded string to json_encode. There is nothing about json on that utf8 thread so i don't think it's a duplicate – Phil May 08 '15 at 20:29
  • the problem is not that i cant print out the characters on the page but that json encoding are ignoring these characters and returning null instead – NorthernLights May 08 '15 at 21:58
  • `json_encode` expects **UTF-8 encoded strings**, it does not work with non-UTF-8 strings. You need to get data from your database in UTF-8 *or* convert it to UTF-8 by hand. The aforelinked UTF-8 question is very apropos. – deceze May 10 '15 at 10:34
  • japp thanks all . the answer in "UTF-8 character encoding battles json_encode()" question solved the problem – NorthernLights May 10 '15 at 12:39

0 Answers0