PHP While While循环:Second While循环不显示数据

时间:2015-07-22 16:02:05

标签: php while-loop

我在While Loop上遇到了麻烦。这是我的代码:

<?php
//DISPLAY'S SKILL LIST
$showItemList = $con->query('SELECT * FROM items');


if ($showItemList->num_rows > 0) {
     $x=1;
     // output data of each row
     while($row = $showItemList->fetch_assoc()) {
         echo '<tr>';
         echo '<td>' . $x++ . '</td>';
         echo '<td>' . $row['item_id'] . '</td>';
         echo '<td>' . $row['item_name'] . '</td>';
         echo '<td>' . $row['item_category'] . '</td>';
         echo '<td>' . $row['item_costprice'] . '</td>';
         echo '<td>' . $row['item_retailprice'] . '</td>';
         echo '<td>' . $row['item_tax'] . '%</td>';
         echo '<td>';
              $showInHouseQty = $con->query('SELECT SUM(item_quantity) AS TotalQuantityInStock FROM item_inventory_inhouse WHERE item_id="'.$row['item_id'].'"');
              while($row = $showInHouseQty->fetch_assoc()) {
                echo $row['TotalQuantityInStock'];
              } 
         echo '</td>';
         echo '<td>'; 
                $showPharmaQty = $con->query('SELECT SUM(item_quantity) AS TotalPharmaQty FROM item_inventory_inpharmacy WHERE item_id="'.$row['item_id'].'"');
              while($row = $showPharmaQty->fetch_assoc()) {
                echo $row['TotalPharmaQty'];
              }
         echo '</td>';                                                                                                                     
         echo '</tr>';
     }     
} 


?>

嗯,问题是,我无法得到$row['TotalPharmaQty'];

的结果

2 个答案:

答案 0 :(得分:-1)

如果在嵌套while循环中使用与父while循环中相同的变量,它们将覆盖。将第二个和第三个while循环中的$row分别更改为$row2$row3,它应该可以正常工作。

答案 1 :(得分:-1)

您正在覆盖$row覆盖父查询的TotalPharmaQty变量,这就是发生此问题的原因