You are using array union operator. From PHP docs:
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.
Your temporary array has the same key as the array you are collecting into. Both have elements with key 0, so the new row is not added.
To fix it you should use array push operator i.e. []
.
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
}
However, I do not recommend such manual approach. mysqli has methods for fetching all the rows at once. You should use fetch_all()
instead.
// 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);
If you really want to loop on the results one by one and build an array manually then use foreach
loop on the mysqli_result object.
// 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);