3
What am I doing
I’m pulling data through PHP with a list of cities to create a chart with Javascript, and I realized that it was not returning the data, so I downloaded the file that takes the data from the database and does the conversion with the json_encode
to see what would happen.
What happened
The method to get the cities is working, and when I give a print_r
in the result it is shown correctly, but as soon as I give a echo json_encode($resultado)
the page is empty.
My code
Calling Method and Parsing
$result = $cidadeDAO->getCidades();
echo json_encode($result);
Method to obtain cities
public function getCidades() {
$stmt = self::$connection->prepare("SELECT cidade FROM cd_unidades order by cidade ASC");
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
Result print_r
Array
(
[0] => Array
(
[cidade] => Araraquara
)
[1] => Array
(
[cidade] => Bauru
)
)
There must be some mistake in the json_encode. Note in the manual that it returns FALSE if an error occurs.
– Franchesco
Use the function
json_last_error
and verify which value is returned. In that reply there is an example withjson_decode
, just adapt it tojson_encode
.– stderr