简体   繁体   中英

PHP/SQL How can I make my query include results that has no records in another table yet?

I have 2 php codes that performs room search. The user specifies no. of beds, check in date and check out date. The first code retrieves all rooms with the corresponding number of beds. The second code checks if the room has an existing booking, if it does and the booking dates do not clash with the user input dates the room is displayed, if it does not have an existing booking it is simply displayed.

My first php code:

$sql = "SELECT rooms.rid, beds, orientation, price FROM rooms
        WHERE (beds = $nOfBeds)";
if ($rOrientation != "") {
$sql .= " AND orientation = '$rOrientation'";
}

$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));

My second php code:

<?php
    while ($row = mysqli_fetch_array($results)) { 

    $sql2 = "SELECT rid, checkin, checkout FROM bookings";

    $results2 = mysqli_query($conn, $sql2)
    or die ('Problem with query' . mysqli_error($conn));

    $row2 = mysqli_fetch_array($results2);

    if ($row["rid"] == $row2["rid"]) {
        if (($cInDate < $row2["checkin"] && $cOutDate <= $row2["checkin"]) ||
           (($cInDate >= $row2["checkout"] && $cOutDate > $row2["checkout"]))) {
?>
            <tr>
                <td><?php echo $row["rid"]?></td>
                <td><?php echo $row["beds"]?></td>
                <td><?php echo $row["orientation"]?></td>
                <td><?php echo $row["price"]?></td>
            </tr>
    <?php }
    } 
    } ?>

currently it is working right in regards to filtering out clashing dates however it is also filtering out rooms that does not have an existing booking. another problem i encountered with this code is it does not seem to work for multiple bookings with the same room id (rid) so right now it is only filtering dates regarding the first booking.

this is my booking table: http://imgur.com/DGOko1f

this is rooms table: http://imgur.com/ED5KFES

You can use,

if($result != NULL) {
   // Execute code
} else {
   // Give NULL values or error
}

Other way is, if(count($result) > 0) .

This way you can get error free results.

nanjero05

Several things with the code. In the second php code your are going to execute a query per each room. That is not so good because it will generate many requests to the database.

Also your are requesting ALL the bookings, and then filtering in memory the ones that corresponds to the room. That it is also not always goods. Your read all the bookings again and again per each room.

One solution is to add in the second query (second php code) a condition to select only the bookings for the current room.

 $rid = $row["rid"]
 $sql2 = "SELECT rid, checkin, checkout FROM bookings where rid = $rid";

(Note: read about SQL injection to improve this)

Then you know that if the $result has no records, there are no bookings for the room.

why you need multiple query try this simple one liner

$sql = "SELECT rooms.rid, beds, orientation, price FROM rooms 
    WHERE beds = $nOfBeds AND rid NOT IN (SELECT bookings.rid FROM bookings WHERE user_checkin between checkin and checkout or ('user_checkin <= checkin AND user_checkout >= checkout)  )";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM