tobias
tobias

Reputation: 2362

PHP Json output with special character

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.

Upvotes: 1

Views: 2587

Answers (2)

Jonathan
Jonathan

Reputation: 2983

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.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

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

Upvotes: 1

Related Questions