Php Mysql表上的循环实际上是如何工作的?

Php Mysql表上的循环实际上是如何工作的?,php,mysql,arrays,foreach,Php,Mysql,Arrays,Foreach,我知道foreach代表什么以及它的用途。但我认为我滥用了它。我在数据库中有一个cart表,当用户通过html表单在网站上添加产品时,该表将动态填充。 我很难在用户端显示产品 使用第一个代码,我可以显示所有行,但不能显示产品信息 <?php require_once 'core/init.php'; $cartQ = $db->query("SELECT * FROM cart"); $result = mysqli_fetch_as

我知道foreach代表什么以及它的用途。但我认为我滥用了它。我在数据库中有一个cart表,当用户通过html表单在网站上添加产品时,该表将动态填充。

我很难在用户端显示产品

使用第一个代码,我可以显示所有行,但不能显示产品信息

   <?php
    require_once 'core/init.php';

        $cartQ = $db->query("SELECT * FROM cart");
        $result = mysqli_fetch_assoc($cartQ);
        $items =  json_decode($result['items'],true);
        $i = 1;
        $sub_total = 0;
        $item_count = 0;

    ?>
      <h2 class="text-center">My Cart</h2>

      <?php if($cartQ->num_rows== 0): ?>
      Your cart is empty.

      <?php elseif($cartQ->num_rows > 0): ?>
              <?php

        foreach ($cartQ as $item) {
                      $product_id = $item['id'];
                      $productQuery = $db->query("SELECT * FROM product WHERE id ='{$product_id}' ");
                      $product = mysqli_fetch_assoc($productQuery);
                 ?>

                    <tr class="p">
        <td class="image"><img src="<?=$product['image_1'];?>" /></td>
        <td class="name"><?=$product['prod_name'];?></td>
        <td class="price"><?=money($product['price']);?></td>
        <td class="quantity"> <?=$item['quantity'];?></td>
        <td class="pricesubtotal"><?=money($item['quantity'] * $product['price'] );?></td>
        <td class=""><div><button name='removeitem' onclick="update_cart('removeitem','<?=$product_id['id'];?>');">&times</button></div></td>
      </tr>    
<?php endif;?>

我的手推车
你的车是空的。

你真的需要遍历整个记录集吗。不,您只需在表之间执行
联接
,即可获得所需的记录,如

SELECT p.* 
FROM product p
JOIN cart c ON p.id = c.id;

你真的需要遍历整个记录集吗。不,您只需在表之间执行
联接
,即可获得所需的记录,如

SELECT p.* 
FROM product p
JOIN cart c ON p.id = c.id;

当您得到一个结果时,PHP/mySql会维护一个游标(这可以是mySql或PHP,具体取决于您的设置),但它们对最终用户的作用是相同的

如果我们为您的代码添加注释,我会更容易:

$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
// CartQ containes a "Query Result" (which may be false if it failed - check for false !)
// At this stage, the "Query Result" is sitting on the first item

$result = mysqli_fetch_assoc($cartQ);
// $result actually contains the first row of data, **and has moved the "Query Result" cursor to the second row** 

if($cartQ->num_rows== 0)
// This does not move the cursor.
所以,如果你想要一个以上的结果,你需要做的是围绕结果进行循环

$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
if ($cartQ == false) {
    // SQL Error - debug me
} else {
    while ($row = mysqli_fetch_assoc($cartQ)) {   
        // Do something with the $row
        // Will keep looping until all rows consumed
        // Won't enter if there are no rows.
    }
    // Cursor now sits on (last+1) row (i.e. off the end).
}


旁注:您可以通过不存储在JSON中的方式在一个查询中“连接表”并完成所有这些操作。将更容易一次说“获取购物车中所有产品的所有产品详细信息”。您只能在mySQL 5.7+中使用JSON,但在这种情况下不使用JSON更有效

当您得到一个结果时,PHP/mySql会维护一个游标(这可以是mySql或PHP,具体取决于您的设置),但它们对最终用户的作用相同

如果我们为您的代码添加注释,我会更容易:

$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
// CartQ containes a "Query Result" (which may be false if it failed - check for false !)
// At this stage, the "Query Result" is sitting on the first item

$result = mysqli_fetch_assoc($cartQ);
// $result actually contains the first row of data, **and has moved the "Query Result" cursor to the second row** 

if($cartQ->num_rows== 0)
// This does not move the cursor.
所以,如果你想要一个以上的结果,你需要做的是围绕结果进行循环

$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
if ($cartQ == false) {
    // SQL Error - debug me
} else {
    while ($row = mysqli_fetch_assoc($cartQ)) {   
        // Do something with the $row
        // Will keep looping until all rows consumed
        // Won't enter if there are no rows.
    }
    // Cursor now sits on (last+1) row (i.e. off the end).
}


旁注:您可以通过不存储在JSON中的方式在一个查询中“连接表”并完成所有这些操作。将更容易一次说“获取购物车中所有产品的所有产品详细信息”。您只能在mySQL 5.7+中使用JSON,但在这种情况下不使用JSON更有效

谢谢你的回答,我会在几秒钟内试一试。在我想说的一切之前,我确实把桌子连在一起了。但我仍然是一个noob,所以我可能在这过程中犯了一些错误。我不是在批评(加入评论),只是想把你推向正确的方向。先得到你所做的(一次一步),然后考虑删除JSON,加入一个中间的“CARTYBETIONS”表。谢谢你的回答,我会在几秒钟内试用它。在我想说的一切之前,我确实把桌子连在一起了。但我仍然是一个noob,所以我可能在这过程中犯了一些错误。我不是在批评(加入评论),只是想把你推向正确的方向。先得到你所做的(一次一步),然后考虑删除JSON,以加入一个中间的“CARTYBETIONS”表。那么你的问题是什么?请检查你的<代码>查询<代码>。当您说“SELECT*FROM cart WHERE id='{$cart\u id}'时,它只选择一个结果。只考虑“选择从购物车”。那么你的问题是什么?请检查你的代码>查询< /代码>。当您说“SELECT*FROM cart WHERE id='{$cart\u id}'时,它只选择一个结果。只考虑“选自车”。