【发布时间】:2020-06-05 23:13:00
【问题描述】:
我是 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);
【问题讨论】:
-
把你的时间改成这个
while($row = $stmt->fetch()){ -
$order_details[] = $row会变魔术。 -
@mitkosoft 他没有覆盖。他只是在滥用数组加法运算符
-
在你的评论中使用
$order_details[array_shift($row)] = $row;在 while 循环 @mitkosoft 中不好 -
@Dlk 你把 PDO 和 mysqli 搞混了
标签: php arrays mysqli append prepared-statement