php fetch\u assoc循环

hjzp0vay  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(251)

我的查询选择了所有符合特定条件的行,但是我在使用fetch\u 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\u assoc函数转换为一个循环,以便在json中获得所有结果,但我不确定如何执行此操作,如有任何帮助或提示,将不胜感激。

b91juud3

b91juud31#

只需使用while循环将行写入数组,然后对其进行编码。因此,请替换此行:

echo json_encode(array(($result->fetch_assoc())));

使用此循环:

$output = array();
while ($row = $result->fetch_assoc()) {
    $output[] = $row;
}
echo json_encode($output);

相关问题