2017-10-29 3 views
2

whileループの助けを借りて、最初のクエリのフェッチ値を2番目のクエリのwhere条件として使用しようとしています。私が欲しいのは: A B C D E のようなすべての値に対して実行します。 最初の値で実行します。 ここに私のコードがあります。クエリ内部whileループは最初の値に対してのみ実行します

<?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

投稿された回答を確認してください – Tarun

答えて

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>"; 
            } 
         } 

       } 
      } 
    ?> 
関連する問題