I have a strange issue. I have a simple PHP script that uses PDO to get all countries from a database then returns the result as json. When I use the fetch function instead of fetchAll everything works as expected. When I print_r the data is there.
Doesn't work:
$sql = "SELECT * FROM countries";
if($stmt = $_db->prepare($sql))
{
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
header("content-type:application/json");
echo json_encode($data);
exit();
}
Works:
$sql = "SELECT * FROM countries";
if($stmt = $_db->prepare($sql))
{
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
header("content-type:application/json");
echo json_encode($data);
exit();
}
Results of print_r:
Array
(
[0] => Array
(
[id] => 1
[name] => Afghanistan
)
[1] => Array
(
[id] => 2
[name] => Åland Islands
)
[2] => Array
(
[id] => 3
[name] => Albania
)
....
[248] => Array
(
[id] => 249
[name] => Zimbabwe
)
)
1
print_rfrom? Is it the same in both?