我在sql数据库中有两个表,第一个是预订,它有客户详细信息,另一个是booking_line,它已经由客户购买了产品历史记录,现在我希望这两个结果都在一个数组中,如下图所示 我只想要一个名为 Customer 的数组,其中包含客户明智的所有购买详细信息
array(
[0] => array(
[id] => 67
[name] => Neha
[mobile_no] => xxxxxxxxx
[gender] => Female
[Sale] => Array
(
[id] => 1
[booking_id] => 67
[barcode] => 1368
[product_name] => Custom Print Mattel Rakhi BE
[p_price] => 35
[price] => 100
)
[Sale] => Array
(
[id] => 2
[booking_id] => 67
[barcode] => 1368
[product_name] => Custom Print Mattel Rakhi BE
[p_price] => 35
[price] => 100
)
)
[1] => array(
[id] => 68
[name] => Nehaxx
[mobile_no] => xxxxxxxxx
[gender] => Female
)
目前我得到这样的输出
array(
[0] => array(
[id] => 67
[name] => Neha
[mobile_no] => xxxxxxxxx
[gender] => Female
)
[1] => array(
[Sale] => Array
(
[id] => 1
[booking_id] => 67
[barcode] => 1368
[product_name] => Custom Print Mattel Rakhi BE
[p_price] => 35
[price] => 100
)
)
[2] => array(
[id] => 68
[name] => Nehaxx
[mobile_no] => xxxxxxxxx
[gender] => Female
)
[1] => array(
[Sale] => Array
(
[id] => 1
[booking_id] => 67
[barcode] => 1368
[product_name] => Custom Print Mattel Rakhi BE
[p_price] => 35
[price] => 100
)
)
)
我的代码是
$sqlA = "SELECT * FROM booking";
$resultA = $conn->query($sqlA);
$dataA = array();
while($rowA = $resultA->fetch_assoc()) {
$sql = "SELECT * FROM booking_line WHERE booking_id=".$rowA["id"]."";
$result = $conn->query($sql);
$dataA[] = $rowA;
while($row = $result->fetch_assoc()) {
$dataA[] = array("Sale" => $row);
}
}
print_r($dataA);
问题是您将第二行数据添加到数组的顶层,而不是作为主行下方的项目。
这应该有帮助:
while($rowA = $resultA->fetch_assoc()) {
$sql = "SELECT * FROM booking_line WHERE booking_id=".$rowA["id"]."";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$rowA["Sale"] = $row;
}
$dataA[] = $rowA;
}