[英]PHP fetch_assoc loop
我的查询选择了符合特定条件的所有行,但是为此使用fetch_assoc时遇到了问题,我只得到一行。
这是我的代码:
$stmt = $conn->prepare("SELECT filename, comment, action FROM files WHERE belongsto = ?");
$stmt->bind_param("s", $_POST['case_identifer']);
$stmt->execute();
$result = $stmt->get_result();
echo json_encode(array(($result->fetch_assoc())));
$stmt->close();
$conn->close();
我需要将fetch_assoc函数转换为循环,以便在JSON中获得所有结果,但是我不确定如何执行此操作,将不胜感激。
只需使用while循环将行写入数组,然后json_encode即可。 因此,请替换此行:
echo json_encode(array(($result->fetch_assoc())));
使用此循环:
$output = array();
while ($row = $result->fetch_assoc()) {
$output[] = $row;
}
echo json_encode($output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.