while循环中的while循环用于从数据库中获取数据

时间:2017-09-25 09:12:52

标签: php sql

我正在尝试从多个SQL表中获取数据(如果它们具有匹配的IMEI)并在html表中显示它们。如果IMEI不匹配,那么我希望它显示' - '在那个盒子里。

为此,我有来自表A(导入)的显示数据,然后尝试匹配IMEI。在这样做时,我使用if else来定义条件,主要问题出现在if和else中。在执行代码时,它将执行else部分之后的if部分,它不遵循条件。

例如:

while ($row = mysqli_fetch_assoc($result)){
...
...
*****************this while shows error
while($row1 = mysqli_fetch_assoc($result1)){
********here after executing if it will execute the else also. it does not follow the condition
if(import_imei == sales_imei){
<td>imei</td>
}else{
<td> - </td>
}
}
...
************the same condition work here perfectly
while($row2 = mysqli_fetch_assoc($result2)){
if(import_imei == ret_imei){
<td>name</td>
}else{
<td> - </td>
}
}

}

以下是代码。  我写了错误&#39;在发生错误的代码中

 $sql ="SELECT model,importDate,IMEI1,IMEI2 FROM mainreport ORDER BY id DESC LIMIT $start_from, $limit";
    $result = mysqli_query($con,$sql);
    while ($cgimport = mysqli_fetch_assoc($result)) {
      $cgimport_modelName = $cgimport['model'];
      $cgimport_importDate = $cgimport['importDate'];
      $cgimport_IMEI1 = $cgimport['IMEI1'];
      $cgimport_IMEI2 = $cgimport['IMEI2'];
      ?>
      <tr>




        <!-- **************   DISPLAYING DATA IN TABLE FROM mainreport **************** -->
        <td style="text-align: center;"><?php echo $cgimport_modelName ;?></td>
        <td style="text-align: center;"><?php echo $cgimport_importDate ;?></td>
        <td style="text-align: center;"><?php echo $cgimport_IMEI1 ;?></td>


        <!-- ************** FETCHING SALES DATA MADE BY CG TO DISTRIBUTOR **************** -->
        <?php

        $sql1 = "SELECT billMiti,invoiceDate,billno,price,nameOfParty,IMEINo FROM salesreport ";
        $result1 = mysqli_query($con, $sql1);
        while ($cgSalesReport = mysqli_fetch_assoc($result1)) {
          $cgSalesReport_BillMiti = $cgSalesReport['billMiti'];
          $cgSalesReport_invoiceDate = $cgSalesReport['invoiceDate'];
          $cgSalesReport_billno = $cgSalesReport['billno'];
          $cgSalesReport_price = $cgSalesReport['price'];
          $cgSalesReport_distName = $cgSalesReport['nameOfParty'];
          $cgSalesReport_IMEINo = $cgSalesReport['IMEINo'];

          // echo $cgimport_IMEI1;

          //******************************************
          //ERROR IN THIS IF 
          //******************************************
          if ( ($cgSalesReport_IMEINo == $cgimport['IMEI1']) || ($cgSalesReport_IMEINo == $cgimport['IMEI2']) ) {

            ?>


            <!-- ************* DISPLAYING TABLE FROM salesreport *********************** -->
            <td style="text-align: center;"><?php echo $cgSalesReport_BillMiti; ?></td>
            <td style="text-align: center;"><?php echo $cgSalesReport_invoiceDate; ?></td>
            <td style="text-align: center;"><?php echo $cgSalesReport_billno; ?></td>
            <td style="text-align: center;"><?php echo $cgSalesReport_price; ?></td>
            <?php


            // *****************  FETCHING DATA FROM cdistributor ****************************
            $sql2 = "SELECT rdCode,name FROM cdistributor ";
            $result2 = mysqli_query($con, $sql2);
            while ($cgDistributor = mysqli_fetch_assoc($result2)) {
              $cgDistributor_rdCode = $cgDistributor['rdCode'];
              $cgDistributor_name = $cgDistributor['name'];



              if ($cgDistributor_name == $cgSalesReport_distName) {
                ?>
                <!-- **************** DISPLAYING TABLE FROM cdistributor ***************** -->
                <td align='center'><?php echo $cgDistributor_rdCode ; ?></td>
                <td align='center'><?php echo $cgDistributor_name ; ?></td>
                <?php
              }
              else{
                 ?>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
                <?php                   
              }
            }
            // ************ END FETCHING cdistributor *******************
          }
          // *************************
          // ERROR IN THIS ELSE
           //*************************
          else{
              ?>
                 <td style="text-align: center;">-</td> 
                 <td style="text-align: center;">-</td> 
              <?php  
          }
        }
        // *************** END FETCHING salesreport *****************            

        ?>

        <!-- *******************  FETCHING DISTRIBUTOR SALES DATA MADE BY DISTRIBUTOR TO RETAILER ****************** -->
        <?php

        $sql3 = "SELECT rinvoiceDate,rbillno,rPrice,rIMEINo,rnameOfParty,distname FROM rrdsalesreport";
        $result3 = mysqli_query($con, $sql3);
        while ($cgDistributorSales = mysqli_fetch_assoc($result3)) {
          $cgDistributorSales_rinvoiceDate = $cgDistributorSales['rinvoiceDate'];
          $cgDistributorSales_rbillno = $cgDistributorSales['rbillno'];
          $cgDistributorSales_rPrice = $cgDistributorSales['rPrice'];
          $cgDistributorSales_rIMEINo = $cgDistributorSales['rIMEINo'];
          $cgDistributorSales_rnameOfParty = $cgDistributorSales['rnameOfParty'];
          $cgDistributorSales_distname = $cgDistributorSales['distname'];

          if ($cgimport_IMEI1 == $cgDistributorSales_rIMEINo || $cgimport_IMEI2 == $cgDistributorSales_rIMEINo) {

            ?>


            <!-- ************* DISPLAYING TABLE FROM rrdsalesreport *********************** -->
            <td style="text-align: center;"><?php echo $cgDistributorSales_rbillno; ?></td>
            <td style="text-align: center;"><?php echo $cgDistributorSales_rinvoiceDate; ?></td>
            <td style="text-align: center;"><?php echo $cgDistributorSales_rPrice; ?></td>
            <?php


            // *****************  FETCHING DATA FROM retailer ****************************
            $sql4 = "SELECT rCode,rname,rd FROM retailer ";
            $result4 = mysqli_query($con, $sql4);
            while ($cgRetailer = mysqli_fetch_assoc($result4)) {
              $cgRetailer_rCode = $cgRetailer['rCode'];
              $cgRetailer_rname = $cgRetailer['rname'];
              $cgRetailer_rd = $cgRetailer['rd'];



              if ($cgRetailer_rname == $cgDistributorSales_rnameOfParty) {
                ?>
                <!-- **************** DISPLAYING TABLE FROM retailer ***************** -->
                <td align='center'><?php echo $cgRetailer_rCode ; ?></td>
                <td align='center'><?php echo $cgRetailer_rname ; ?></td>
                <?php
              }
              else{
                 ?>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
                <?php                   
              }
            }
            // ************ END FETCHING retailer *******************
          }else{
            ?>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
                <td style="text-align: center;">-</td>
            <?php
          }
        }
        // *************** END FETCHING rrdsalesreport *****************
        ?>

        <!-- ***************** FETCHING DATA FROM activereport ******************** -->
        <?php
        $sql5 = "SELECT aIMEI,dateOfActivation FROM activereport";
        $result5 = mysqli_query($con, $sql5);
        while ($cgActivereport = mysqli_fetch_assoc($result5)) {
          $cgActivereport_aIMEI = $cgActivereport['aIMEI'];
          $cgActivereport_dateOfActivation = $cgActivereport['dateOfActivation'];
          if ($cgimport_IMEI1 == $cgActivereport_aIMEI || $cgimport_IMEI2 == $cgActivereport_aIMEI) {
            ?>
            <td align='center'><?php echo $cgActivereport_dateOfActivation ; ?></td>
            <?php
          }
        }

        // *************  END OF FETCHING activereport ***************

        ?>

      </tr>

      <?php
    } //************ END FETCHING MAINREPORT***********************

    ?>

1 个答案:

答案 0 :(得分:1)

$sql = "Your First Query..";

                $result = mysql_query($sql);

                $jsonData = array();

while ($array = mysql_fetch_assoc($result)) {

                        $follower_id=$array['to_follow_user_id']
                        $SQL_ONE = "Your Second Query.....";
                            $result_one = mysql_query($SQL_ONE);
                            while ($array_one = mysql_fetch_assoc($result_one)) {
                                $jsonData[] = $array_one;
                            }


                }

我想你想要上面的东西......