为什么只有第一个mysqli_fetch()结果存储在数组中

问题描述 投票:0回答:1

我是mysqli准备好的语句的新手。我试图将结果存储在关联数组中,以便可以进一步使用它。在追加到数组之前,将正确打印结果,但在追加时,仅添加第一个条目。这里的方法有什么错误?

// order_details_table
$order_details = array();
$invoice = 1234;
$stmt = $con->prepare("SELECT `description`,`amount` FROM order_details_table WHERE invoice_no = ?");
$stmt->bind_param("s", $invoice);
$stmt->execute();
$stmt->bind_result($description,$amount);
while($stmt->fetch())
{
    print_r($description." ".$amount); //This prints all the entries for the given query
    $row['desc'] = $description;
    $row['amount'] = $amount;
    $order_details += [$row]; //This appends just the first entry
}
print_r($order_details);
php arrays mysqli append prepared-statement
1个回答
0
投票

使用foreach($ stmt-> fetch()作为$ row)

© www.soinside.com 2019 - 2024. All rights reserved.