我无法让我的脚本工作,我不明白为什么。我想获取所有 SQL 结果并将它们放入一个数组中。我当前的脚本只返回一个结果。我知道我需要使用循环,但我只是不知道如何将它与当前脚本集成。我已经阅读了 50 多篇文章,但我仍然无法工作。    <?php$sql = "SELECT * FROM cart WHERE sess_id = '$sess_id'";$result = mysqli_query($conn, $sql);$count= mysqli_num_rows($result);      $items= array();    if (mysqli_num_rows($result) > 0) {    // output data of each row     while($row = mysqli_fetch_assoc($result)) {       for($i=0;$i<$count;$i++)    {      $items = "{sku: "."'".$row["prod_sku"]."'".", quantity : ".$row["prod_qty"]."}, <br>";     }                                              }                                   }                   echo $items."<br>";          ?>
                    
                    
                1 回答
 
                    
                    
                            慕桂英546537
                            
                                
                            
                        
                        
                                                
                    TA贡献1848条经验 获得超10个赞
它既简单又简单,您不需要在 while 循环内使用 for 循环。请使用以下代码,希望对您有所帮助。
$sql = "SELECT * FROM cart WHERE sess_id = '$sess_id'";
$result = mysqli_query($conn, $sql);
$items= array();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$items[] = "{sku: "."'".$row["prod_sku"]."'".", quantity : ".$row["prod_qty"]."}, <br>";
}
}
// to print the array use the following command
// print_r($items);
// to echo the $items variable, you may encode it by json
echo json_encode($items);
谢谢。
- 1 回答
- 0 关注
- 199 浏览
添加回答
举报
0/150
	提交
		取消
	