我有这个查询

SELECT json_object('image_name', image_name, 'iid', iid) FROM `posts_images` WHERE `pid` = '62'


导致这个

Image

我可以在同一查询中将这些结果转换为一行json数组吗?

最佳答案

您可以执行以下操作:

$query = "SELECT json_object('image_name', image_name, 'iid', iid) FROM `posts_images` WHERE `pid` = '62'";
$result = mysqli_query($query);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}
print json_encode($data);

关于mysql - MySQL将json_objects转换为数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44925217/

10-13 02:09