For some reason the item \"description\" returns NULL
with the following code:
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.
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:
To get UTF-8 charset you can specify that in the DSN.
$link = new PDO("mysql:host=localhost;dbname=DB;charset=UTF8");
I bet you are retrieving data in non-utf8 encoding: try to put mysql_query('SET CHARACTER SET utf8')
before your SELECT
query.
AHHH!!! This looks so wrong it hurts my head. Try something more like this...
<?php
include('db.php');
$result = mysql_query('SELECT `id`, `name`, `description`, `icon` FROM `staff` ORDER BY `id` DESC LIMIT 20') or die(mysql_error());
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
echo json_encode($rows);
?>
mysql_num_rows
you should use <
not <=
. You should also cache this value (save it to a variable) instead of having it re-count every loop. Who knows what it's doing under the hood... (might be efficient, I'm not really sure)mysql_fetch_array
returns the values both by key
and by int
. You not using the indices, so don't fetch em.If this really is a problem with json_encode
, then might I suggest replacing the body of the loop with something like
$rows[] = array_map('htmlentities',$row);
Perhpas there are some special chars in there that are mucking things up...