我试图使用第一个查询的fetch值作为第二个查询的where条件,并借助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>";
}
}
}
}
?> 发布于 2017-10-29 06:54:03
您正在用内循环的结果集覆盖外部循环的结果集。您可以按照以下方式更正代码:
<?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>";
}
}
}
}
?> https://stackoverflow.com/questions/46997575
复制相似问题