For some reason the item "description" returns NULL
with the following code:
<?php
include('db.php');
$result = mysql_query('SELECT * FROM `staff` ORDER BY `id` DESC LIMIT 2') or die(mysql_error());
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
echo json_encode($rows);
?>
Here is the schema for my database:
CREATE TABLE `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` longtext COLLATE utf8_unicode_ci,
`description` longtext COLLATE utf8_unicode_ci,
`icon` longtext COLLATE utf8_unicode_ci,
`date` longtext COLLATE utf8_unicode_ci,
`company` longtext COLLATE utf8_unicode_ci,
`companyurl` longtext COLLATE utf8_unicode_ci,
`appurl` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is what is echoed out on the page:
[{"id":"4","name":"Noter 2","description":null,"icon":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","date":"1262032317","company":"dBelement, LLC","companyurl":"http:\/\/dbelement.com\/","appurl":"http:\/\/noter2.dbelement.com"},{"id":"3","name":"Noter 2","description":null,"icon":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","date":"1262032317","company":"dBelement, LLC","companyurl":"http:\/\/dbelement.com\/","appurl":"http:\/\/noter2.dbelement.com"}]
Any ideas?
ntd's anwser didn't solve my problem. For those in same situation, here is how I finally handled this error: Just utf8_encode each of your results.
Hope it helps!
For anyone using PDO, the solution is similar to ntd's answer.
From the PHP PDO::__construct page, as a comment from the user Kiipa at live dot com:
few day ago I have the SAME problem with 1 table.
Firstly try:
If last line returns 5, problem is with your data. I know, your tables are in UTF-8, but not entered data. For example the input was in txt file, but created on Win machine with stupid encoding (in my case Win-1250 = CP1250) and this data has been entered into the DB.
Solution? Look for new data (excel, web page), edit source txt file via PSPad (or whatever else), change encoding to UTF-8, delete all rows and now put data from original. Save. Enter into DB.
You can also only change encoding to utf-8 and then change all rows manually (give cols with special chars - desc, ...). Good for slaves...
The PHP.net recommended way of setting the charset is now this:
mysqli_set_charset('utf8')
For me, an issue where json_encode would return null encoding of an entity was because my jsonSerialize implementation fetched entire objects for related entities; I solved the issue by making sure that I fetched the ID of the related/associated entity and called ->toArray() when there were more than one entity associated with the object to be json serialized. Note, I'm speaking about cases where one
implements JsonSerializable
on entities.You should pass utf8 encoded string in json_encode. You can use
utf8_encode
andarray_map()
function like below: