[英]how to echo specific columns of database using while loop with 3 different tables
我想從 3 個不同的表中選擇不同的列並在表數據中顯示。
我有一張名為 room 的桌子。 因為我有兩個外鍵,分別命名為 Hotel_id 和 Room_Type_id 。 房間表中的其他列是價格,設施等。我想使用 FK hotel_id 和 room_type 表使用 FK Room_Type_id 從酒店表中獲取 hotel_name。
我進行了很多頭腦風暴,並使用連接進行單獨的查詢以查找hotel_name 和room_type_name。 使用以下查詢
//Query to find hotel_name
$query = "SELECT hotels.hotel_name FROM hotels INNER JOIN room ON room.Hotel_Id=hotels.hotel_id";
$res = mysqli_query($connection, $query);
while ($data1 = mysqli_fetch_array($res)) {
//Query to find room_type_name:
$sel_cus = "SELECT room_type.Room_Type_Name FROM room INNER JOIN room_type ON room.room_type=room_type.Room_Type_Id";
$res_cus = mysqli_query($connection, $sel_cus);
while ($data = mysqli_fetch_array($res_cus)) {
//Query to select price,and picture of room:
$count=1; // counter to print serial numbers in table
$sel_cus = "select * from room Order by room_id ASC";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
// here is code to print record in table
<tbody>
<tr>
<td><?php echo $count; ?></td>
<td ><?php echo $data1['hotel_name'];?></td>
<td><?php echo $data['Room_Type_Name']; ?></td>
<td><?php echo $row['price']; ?></td>
} // End Query to find hotel_name
} // End Query to find room_type_name:
} // end of Query to select price,and picture of room
上面的代碼根據房間表中的記錄迭代hotel_name和room_type_name,目前房間表中有2條記錄,它迭代記錄2次,請幫我解決這個問題。
用上面的代碼輸出:
Sr hotel_name Roomtype price picture
1 ABC Delux 600 a.jpg
2 ABC Delux 600 a.jpg
3 XYZ Super 8000 bcv.jpg
4 XYZ Super 8000 bcv.jpg
I want the output like below
Sr hotel_name Roomtype price picture
1 ABC Delux 600 a.jpg
2 XYZ Super 8000 bcv.jpg
請幫忙
您可以只使用一個查詢,例如:
SELECT hotels.hotel_name
, room_type.Room_Type_Name
, room.price
, room.facilities
, room.picture
FROM hotels
INNER JOIN room ON room.Hotel_Id=hotels.hotel_id
INNER JOIN room_type ON room.room_type=room_type.Room_Type_Id
ORDER BY hotels.hotel_name , room_type.Room_Type_Name, room.price
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.