我试图使用第一个查询的获取值作为第二个查询,其中条件有while循环的帮助。我想要的是: 运行所有可能的值,如A B C D E. 我得到的是: 仅运行第一个值A. 这是我的代码。
<?php
$qu="Select DISTINCT Branch from branch";
$res=mysqli_query($con,$qu);
if($res && mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
{
$br=$row['Branch'];
$qu="select count(P_no) as pick from add_new_patient where Branch='$br' and Result='POSITIVE'";
$res=mysqli_query($con,$qu);
if($res && mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
{
$rs=$row['pick'];
echo "<tr><td>$br</td><td>$rs</td>";
}
}
}
}
?>
答案 0 :(得分:0)
您正在使用内部循环的结果集覆盖外部循环的结果集。您可以按照以下方式更正您的代码:
<?php
$qu="Select DISTINCT Branch from branch";
$res=mysqli_query($con,$qu);
if($res && mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
{
$br=$row['Branch'];
$qu="select count(P_no) as pick from add_new_patient where Branch='$br' and Result='POSITIVE'";
$res2=mysqli_query($con,$qu); // Here you were overwriting
if($res2 && mysqli_num_rows($res2)>0)
{
while($row=mysqli_fetch_assoc($res2))
{
$rs=$row['pick'];
echo "<tr><td>$br</td><td>$rs</td>";
}
}
}
}
?>