I am trying to JSON_encode data from a MySQL database. The characters in the DB are as should.
For example, some of the data contains this: è
When I print out to the browser, this is what I get for all items that have special characters:
{"category":null}
Here is the very basic watered down version of the code I am using to test:
$sql = mysql_query("SELECT category FROM master_cat ORDER BY category ASC");
while ($row = mysql_fetch_assoc($sql))
$output[] = $row;
print(json_encode($output));
How can I get actual data to appear in browser when I am reading the JSON on the web page and not null?
You could use
utf8_encodeon the text fields, but can you check that the data is, indeed, there? Maybe output the data in HTML instead of JSON-encoding it? Because on my system UTF8 is either recognized, gives error, or some characters are truncated.NULL smells of database data missing.
If the data is there, then:
You might want to specify explicitly the MIME type and charset too:
see What is the correct JSON content type?