我试过这个代码但我不能
$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);
?>
结果
{"movies":{"name":"5","0":"5"},"0":null}
我想要的{"电影":{"名称":"5"}]}
移除array_push()
并将[]
添加到$data['movies'] = $result;
以创建子阵列
同时添加
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
或
$row->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
以停止结果集同时返回对象/数组和对每列的数字引用。其中一个应该是连接脚本的一部分
$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);
或者
$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);