第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將多個(gè) SQL 結(jié)果放入數(shù)組中

如何將多個(gè) SQL 結(jié)果放入數(shù)組中

PHP
開(kāi)滿天機(jī) 2022-07-09 16:13:00
我無(wú)法讓我的腳本工作,我不明白為什么。我想獲取所有 SQL 結(jié)果并將它們放入一個(gè)數(shù)組中。我當(dāng)前的腳本只返回一個(gè)結(jié)果。我知道我需要使用循環(huán),但我只是不知道如何將它與當(dāng)前腳本集成。我已經(jīng)閱讀了 50 多篇文章,但我仍然無(wú)法工作。    <?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貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

它既簡(jiǎn)單又簡(jiǎn)單,您不需要在 while 循環(huán)內(nèi)使用 for 循環(huán)。請(qǐng)使用以下代碼,希望對(duì)您有所幫助。


$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);  

謝謝。


查看完整回答
反對(duì) 回復(fù) 2022-07-09
  • 1 回答
  • 0 關(guān)注
  • 181 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)