为什么只有第一个 mysqli_fetch() 结果存储在数组中
why does only first mysqli_fetch() result gets stored in array
我是 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);
使用 foreach($stmt->fetch() as $row)
您正在使用数组联合运算符。来自 PHP 文档:
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
您的临时数组与您要收集到的数组具有相同的键。两者都有键为 0 的元素,因此不会添加新行。
要修复它,您应该使用数组推送运算符,即 []
。
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; // Append the new row into the array
}
但是,我不推荐这种手动方式。 mysqli 具有一次获取所有行的方法。您应该改用 fetch_all()
。
// order_details_table
$invoice = 1234;
$stmt = $con->prepare("SELECT `description`,`amount` FROM order_details_table WHERE invoice_no = ?");
$stmt->bind_param("s", $invoice);
$stmt->execute();
$result = $stmt->get_result(); // Get the result instead of binding
$order_details = $result->fetch_all(MYSQLI_ASSOC);
如果你真的想一个一个地循环结果并手动构建一个数组,那么在 mysqli_result 对象上使用 foreach
循环。
// order_details_table
$invoice = 1234;
$stmt = $con->prepare("SELECT `description`,`amount` FROM order_details_table WHERE invoice_no = ?");
$stmt->bind_param("s", $invoice);
$stmt->execute();
$result = $stmt->get_result(); // Get the result instead of binding
$order_details = []; // Instanciate empty array
foreach($result as $row)
{
$newrow['desc'] = $row['description'];
$newrow['amnt'] = $row['amount'];
$order_details[] = $newrow; //This appends just the first entry
}
print_r($order_details);
我是 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);
使用 foreach($stmt->fetch() as $row)
您正在使用数组联合运算符。来自 PHP 文档:
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
您的临时数组与您要收集到的数组具有相同的键。两者都有键为 0 的元素,因此不会添加新行。
要修复它,您应该使用数组推送运算符,即 []
。
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; // Append the new row into the array
}
但是,我不推荐这种手动方式。 mysqli 具有一次获取所有行的方法。您应该改用 fetch_all()
。
// order_details_table
$invoice = 1234;
$stmt = $con->prepare("SELECT `description`,`amount` FROM order_details_table WHERE invoice_no = ?");
$stmt->bind_param("s", $invoice);
$stmt->execute();
$result = $stmt->get_result(); // Get the result instead of binding
$order_details = $result->fetch_all(MYSQLI_ASSOC);
如果你真的想一个一个地循环结果并手动构建一个数组,那么在 mysqli_result 对象上使用 foreach
循环。
// order_details_table
$invoice = 1234;
$stmt = $con->prepare("SELECT `description`,`amount` FROM order_details_table WHERE invoice_no = ?");
$stmt->bind_param("s", $invoice);
$stmt->execute();
$result = $stmt->get_result(); // Get the result instead of binding
$order_details = []; // Instanciate empty array
foreach($result as $row)
{
$newrow['desc'] = $row['description'];
$newrow['amnt'] = $row['amount'];
$order_details[] = $newrow; //This appends just the first entry
}
print_r($order_details);