更新时间:2023-01-17 15:38:51
$ list必须是一个数组,您可以像下面的代码中那样将项目推入其中:
$list needs to be an array, and you can just push items to it like in this code:
$list = array();
while($stmt->fetch()){
$list[] = array('id' => $fid, 'name' => $fname);
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
您还可以使用方法 fetch_all()来获取所有行一次,而不是循环遍历.尽管在此示例中,您将获得所选的所有字段,而不仅仅是ID和名称.
You could also use the method fetch_all() to get all rows at once, instead of iterating with a loop. Although in this example you'd get all fields that you've selected, instead of just id and name.
$list = $stmt->fetch_all();
$stmt->free_result();
$stmt->close();
echo json_encode($list);