我得到這樣的例子:從查詢創建在一個while循環的關聯數組
<?php
$shop = array(array(Title => "rose",
Price => 1.25,
Number => 15
),
array(Title => "daisy",
Price => 0.75,
Number => 25,
),
array(Title => "orchid",
Price => 1.15,
Number => 7
)
);
?>
但我怎麼可以讓其中的一個結構如下:
$result2 = mysqli_query($con, "SELECT r.id AS id, CONCAT(g.fname,' ',g.lname) AS name,
r.arrival AS arrival, r.departure AS departure
FROM reservations r JOIN guests g ON r.guest = g.id
WHERE r.unit = ".$unit_id."");
while ($row2 = mysqli_fetch_assoc($result2))
{
$reservations = array( array (id => $row['id'],
name => $row['name'],
arrival => $row['arrival'],
departure => $row['departure'],
));
}
的查詢將返回'行x'數。所以我想說明這一點。對我來說,這看起來像是每次迭代都會重新創建數組,並且在完成時只包含查詢中的最後一行。 –
如果這是從查詢返回多少行,它將創建帶有'x'個元素的$保留。 – mseifert
好的。如何訪問一行?例如,在我的第一個示例中,人們可以像這樣訪問數據: 'for($ row = 0; $ row <3; $ row ++) { echo $ shop [$ row] [「Title」]。「costs 「$ shop [$ row] [」Price「]」,你就會得到「$ shop [$ row] [」Number「]; echo「
」; }' –