我如何将mysql结果作为数组会话

时间:2017-09-29 07:52:13

标签: mysql foreach

我有一个页面是[“cart_array”] mange会话来获取购物车中的总价格和总数量我的问题是我试图将mysql结果预先作为会话进入[“cart_array”]所以我可以得到总价和总数量形式mysql获取结果我试过:

<?php 
  $sqli = mysqli_query("SELECT * FROM `books` WHERE `book`='$book'"); 
  $productCount = mysql_num_rows($sqli); // count the output amount
  if($productCount  0) { 
   while($row = mysqli_fetch_array($sqli)){ 
    foreach($row as $v);
   } 
   $id = $v["item_name"]; 
   $product_name = $v["book"]; 
   $price = $v["quantity"]; 
   $_SESSION["cart_array"] = array(0 => array("item_id" => $id, "quantity" => $price)); ?>

提前致谢

1 个答案:

答案 0 :(得分:0)

$sqli = mysqli_query("SELECT * FROM `books` WHERE `book`='$book'"); 
$productCount = mysql_num_rows($sqli); // count the output amount

while($row = mysqli_fetch_array($sqli)){ 
    foreach($row as $v) {
      //$id = $v["item_name"]; //mybe id
      $_SESSION["cart_array"][] =  array(
         "item_id" =>  $v["id"], 
         "quantity" => $v["quantity"], 
         "product_name" => $v["book"]
        );
    }
} 

我现在不在,但是在您的代码中,某些循环为空(foreach($row as $v);),而$_SESSION["cart_array"]现在将是数组;