gpt4 book ai didi

php - 当我使用嵌套 while 循环从另一个表获取数据时,while 循环只显示一条记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:14 27 4
gpt4 key购买 nike

我有案件经理表,我在其中插入法院表 ID 作为外键。我想从两个表中获取记录。使用嵌套 while 循环时,它只显示一行数据。

$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}

最佳答案

因为你正在覆盖你 $result1 将内部查询结果更改为 $result2 然后尝试

$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}

关于php - 当我使用嵌套 while 循环从另一个表获取数据时,while 循环只显示一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45860028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com