1

I am using this PHP script

<?php
mysql_connect("blabla", "blabla", "blabla") or die(mysql_error());
mysql_select_db("blabla") or die(mysql_error());

    $q=mysql_query("SELECT * FROM table ORDER BY id DESC");


 while($e=mysql_fetch_assoc($q))
             $output[]=$e;
       print(json_encode($output));
       mysql_close();
?>

The output is Json. In the mySQL database there are entries with "ü,ä, ö" (German) For entries containing an "ü, ä, ö" the value is "null". I cannot figure out what is the problem with these characters.

11
  • When json_encode returns not a string an error occured. Check if it returns NULL and handle the error condition. To find out which error happened, use json_last_error. Commented Dec 28, 2011 at 23:18
  • 1
    Which encoding has the data that comes from the database? Probably the only thing you need to do is to make the connection charset UTF-8. Commented Dec 28, 2011 at 23:19
  • in the mysql db "latin1_general_ci" is used Commented Dec 28, 2011 at 23:21
  • In the db, okay. And for the connection to the db when you pull the data (mysql changes it on the fly so to meet the database clients expectation (that's your PHP script)). Commented Dec 28, 2011 at 23:24
  • 1
    Try: echo mysql_client_encoding(); after the mysql_connect() line ref - it will tell you the client encoding use. With mysql_set_charset('UTF8'); you should be able to change it if it's not UTF8. json_encode needs UTF8 encoded data. But add in any case what the echo'ed value is. Commented Dec 28, 2011 at 23:27

2 Answers 2

1

Call utf8_encode on each string value before passing them to json_encode.

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

7 Comments

Can you give me a hint how to realize it. I have added my PHP code above.
Replace $output[]=$e; with $output[] = is_string($e) ? utf8_encode($e) ? $e;
ok, we have to replace the second ? with a : But I stil get "null" instead of e.g. the word "für"
Oops, sorry for the typo. Also, I see your elements are arrays. You need a nested solution. Okay, go back to $output[]=$e;, and then just before the print(), add this code: array_walk_recursive($output,function(&$a) {if(is_string($a)) $a = utf8_decode($a);});
This gives following error: Parse error: syntax error, unexpected T_FUNCTION in /home/a80/public_html/get.php on line 17
|
0

Hade the same issue with swedish charset from php to js via ajax and json, values containing any chars like ÅÄÖ got assigned null. The solution turned out to be fairly simple:

  1. Log into php myadmin
  2. Change MySQL connection collation to utf8_swedish_ci (or your language equivalence, my guess would be utf8_unicode_ci for german but you probably know better).
  3. Delete locally stored data such as cache, cookies, and localStorage and wholla it works!

Consequently, the key here is to make sure to set the correct MySQL connection collation in the home page of phpmyadmin (or similar) and repetitively use the same collation throughout your db.

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.