1

Saving data via PHP 5.5 on WAMP to MySQL saves fine.

Recalling it to the client via json_encode did work until I dropped and entered new data into my db. Then json_encode returned nothing - no error - no data. No error in log file.

New data has German street names (with umlauts etc)

I replaced German street names with ascii codes.

json_encode worked as I expected, thus problem sort of resolved.

How does one resolve the issue going forward?

Data in my MySQL INNODB is saved as latin1.

Do I need to filter the data after read from DB, before calling json_encode ? Some other way?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • use utf8 instead of latin1 in db ... if you mixed up codings you have to use uft8_decode utf8_encode etc .... to prepeare data – donald123 Jun 30 '15 at 10:57
  • @donald123 I think you might be right... I just found this article from 2010 and comments from Matteo were leading me in the direction you have suggested. I don't know how to give you a "+1" but I do think you are right. Thanks for the prompt reply! http://stackoverflow.com/questions/4466259/how-to-retrieve-utf-8-data-with-php-and-show-the-correct-encoding-in-an-excelshe and –  Jun 30 '15 at 11:00
  • [Docs](http://php.net/json_encode) mention two things: 1) input needs to be UTF-8 2) Returns `FALSE` on failure — I suspect you just plugged the function into your code, see it didn't crash the site badly and assumed you were done ;-P – Álvaro González Jun 30 '15 at 12:16
  • See http://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279#279279 -- If that is not sufficient, please provide an example, together with `SELECT col, HEX(col) FROM table WHERE ...` to see whether it was even stored correctly. – Rick James Jun 30 '15 at 16:39
  • `äöü`: hex for latin1: `E4 F6 FC`; hex for utf8: `C3A4 C3B6 C3BC` – Rick James Jun 30 '15 at 16:41

1 Answers1

0

Use below code:

mysql_query('SET CHARACTER SET utf8');

Before sql query,

And use below for json encode:

json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)
Amrut Gaikwad
  • 524
  • 1
  • 3
  • 19
  • I just gave you the +1 but it says I'm too new around here for it to be made public. Thanks for your help! –  Jun 30 '15 at 11:54
  • Of course, a partial switch to UTF-8 in an existing Latin-1 site can break other things if used without actually knowing what it means. – Álvaro González Jun 30 '15 at 12:18
  • @AmrutGaikwad Sorry for the delay in accepting your solution as it works very well. Thanks again! –  Jul 22 '15 at 11:57