Reputation: 55
i tried this code but I can't
$row=$dbconnection->prepare ("select * from fa");
$row->execute();
$data=array();
foreach ($row as $result)
{
$data['movies'] = $result;
array_push ($data, $isonformat);
}
echo json_encode ($data);
?>
result
{"movies":{"name":"5","0":"5"},"0":null}
what I want {"movies": [ { "name":"5"} ] }
Upvotes: 0
Views: 66
Reputation: 94662
Remove the array_push()
and add an []
to the $data['movies'] = $result;
to create a sub array
Also add
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
or
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
to stop the resultset returning both object/array and numeric reference to each column. One of these should really be part of the connection script
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$row=$dbconnection->prepare ("select * from fa");
$row->execute();
$data=array();
foreach ($row as $result) {
$data['movies'][] = $result;
}
echo json_encode ($data);
Alternatively
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$row = $db->prepare ("select * from articles");
$row->execute();
$data = [];
$data['movies'] = $row->fetchAll();
echo json_encode ($data);
Upvotes: 1