【问题标题】:While loop only finding the last iterationWhile循环只找到最后一次迭代
【发布时间】:2016-04-23 16:16:22
【问题描述】:

因此,对于学校,我正在尝试建立一个网上商店(这是一个项目),对于购物车,我使用一个数据库,在其中我根据用户获取信息并打印出来。然而,我的 while 循环只找到最后一项,而不是其他两项。现在我很确定旧数据已被覆盖,因此不会被打印,但我不知道如何解决这个问题

这是我目前得到的:

<?php
if(isset($_SESSION['userid'])){

    $db = new PDO('mysql:host=localhost;dbname=ismsite', 'root', 'e-scooter');
    $result = $db->prepare("SELECT * FROM cart where user_id=:userid ORDER BY cart_id DESC LIMIT 3 ");
    $result->bindParam(':userid', $_SESSION['userid']);
    $result->execute();
    $info = array();

    while($row = $result->fetch(PDO::FETCH_ASSOC)){

        $pid = $row['product_id'];
        $quantity = $row['quantity'];

        $result = $db->prepare("SELECT * FROM Products WHERE productID=:pid");
        $result->bindParam(':pid', $pid);
        $result->execute();

        while($row = $result->fetch(PDO::FETCH_ASSOC)){
            echo $row['naam'];
        }

    }
}
else{

    echo "Je moet inloggen om te kunnen winkelen.";

}
?>

谁能帮我解决这个问题?

【问题讨论】:

  • 您在两个循环中都使用$row$result,因此内部循环中的值会覆盖外部循环中的值.....一定会导致问题......如果你必须像这样嵌套循环,为每个循环使用不同的变量名(至少对于$result
  • 如果您学会了使用 SQL JOIN,那么您甚至不需要嵌套循环
  • 我不知道sql join,对我来说只是第一年的第二个学期
  • 但是你可以自己做一些阅读/学习,对吧?
  • 尝试使用此查询而不是您的主要查询。删除第二个查询和第二个 while 循环。 echo $row['naam'] 看看你得到了什么结果 SELECT c.*, p.* FROM cart c INNER JOIN Products p ON p.product_id = c.product_id where user_id=:userid ORDER BY cart_id DESC LIMIT 3 我假设 product_id 是两个表中的一个字段。

标签: php mysql pdo while-loop fetch


【解决方案1】:

您的变量名称有冲突。 试试这个:

if(isset($_SESSION['userid'])){
    $db = new PDO('mysql:host=localhost;dbname=ismsite', 'root', 'e-scooter');
    $cart = $db->prepare("SELECT * FROM cart where user_id=:userid ORDER BY cart_id DESC LIMIT 3 ");
    $cart->bindParam(':userid', $_SESSION['userid']);
    $cart->execute();
    $info = array();

    while($row = $cart->fetch(PDO::FETCH_ASSOC)){
        $pid = $row['product_id'];
        $quantity = $row['quantity'];
        $products = $db->prepare("SELECT * FROM Products WHERE productID=:pid");
        $products->bindParam(':pid', $pid);
        $products->execute();

        while($product = $products->fetch(PDO::FETCH_ASSOC)){
              echo $product['naam'];
        }
}

【讨论】:

    【解决方案2】:

    您的代码覆盖了像$result,$row 这样的变量。在内部循环中,您覆盖了 $row 和 $result 所以您的外部 While 循环不能正常工作。

         while($row = $result->fetch(PDO::FETCH_ASSOC)){
                                        $pid = $row['product_id'];
                                        $quantity = $row['quantity'];
    
                                        $result1 = $db->prepare("SELECT * FROM Products WHERE productID=:pid");
                                        $result1->bindParam(':pid', $pid);
                                        $result1->execute();
    
                                        while($row1 = $result1->fetch(PDO::FETCH_ASSOC)){
                                            echo $row1['naam'];
                                        }
    }
    

    【讨论】:

      【解决方案3】:

      你应该使用SQL JOIN

      试试这个方法:-

       $db = new PDO('mysql:host=localhost;dbname=ismsite', 'root', 'e-scooter');
                   $sql = "SELECT cart.*,Products.* FROM cart 
                          LEFT JOIN Products
                          ON cart.product_id = Products.productID
                          where cart.user_id=:userid ORDER BY cart.cart_id DESC LIMIT 3"; 
                      $result = $db->prepare($sql);
                      $result->bindParam(':userid', $_SESSION['userid']);
                      $result->execute();                   
      
                      while($row = $result->fetch(PDO::FETCH_ASSOC)){
                         echo $row['naam'];
                      }
      

      【讨论】:

      • 这仍然只打印最后一项
      【解决方案4】:

      使用 for 循环代替 while 循环

      for($i = 0, $c = count($result->fetch(PDO::FETCH_ASSOC)); $i < $c; $i++){
          //Do stuff here
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-23
        • 1970-01-01
        • 2012-05-28
        • 2016-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        • 2015-10-20
        相关资源
        最近更新 更多