while 循环只找到最后一次迭代

While loop only finding the last iteration

因此,对于学校,我正在尝试建立一个网上商店(这是一个项目),对于购物车,我使用一个数据库,从中获取基于用户的信息并打印出来。但是我的 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.";

}
?>

谁能帮我解决这个问题?

你应该使用 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'];
                }

使用 for 循环代替 while 循环

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

您的代码覆盖了 $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'];
                                    }
}

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

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'];
        }
}