首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只对第一个值运行循环内部的查询。

问只对第一个值运行循环内部的查询。
EN

Stack Overflow用户
提问于 2017-10-29 06:51:25
回答 1查看 61关注 0票数 2

我试图使用第一个查询的fetch值作为第二个查询的where条件,并借助while循环。我想要的是:运行所有可能的值,像A,B,C,D,E,我得到的是:只运行第一个值A。这是我的代码。

代码语言:javascript
复制
  <?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>";
                                  }
                        }   

                 }
            }
     ?> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-29 06:54:03

您正在用内循环的结果集覆盖外部循环的结果集。您可以按照以下方式更正代码:

代码语言:javascript
复制
<?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>";
                                  }
                        }   

                 }
            }
     ?> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46997575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档