SQL sorgusu ile yardımcı

4 Cevap php

I'm stuck in trying to solve this problem for a days. I even give up for a while, and since i'm a PHP newbie, i need help.

This is my problem: I have a query that selects all appropriate record in a table 'hotels' and then for each hotel looks for booked room of certain type in table 'booked_rooms' and all of that for certain period. So first i'm taking out all hotel_ids from 'hotel_table', based on the location provided from the search form, and for each hotel_id i loop through the 'booked_rooms' table.

İşte kod:

if(isset($_GET['book'])){
$sql=mysql_query("SELECT hotel_id FROM 'hotels' WHERE city='$city") or   die(mysql_error());
while($row=mysql_fetch_array($sql)){
   $sql_2=mysql_query("SELECT * FROM `booked_rooms` WHERE hotel_id='$hotel_id'
   AND   arrival_date BETWEEN '$arrival_date' AND '$departure_date'
   OR departure_date BETWEEN '$arrival_date' AND '$departure_date'")
or die(mysql_error()); 
}

while($row_2=mysql_fetch_array($sql_2)){
   print_r($row_2);
}

}
// $city, $arrival_date and $departure date are values retrieved from the search form

The problem is that i get a loop through 'hotel' table and get all the hotel_ids appropriate to the location, but got nothing with printing the $row_2 array. I tried using JOINS in the SQL, 'foreach' loop, but no luck as well. I know it's a trivial questions, but i'm still learning, so any tip is highly welcomed. Thanks in advance.

4 Cevap

PHP bilmeden, tek bir sorguda bunu yapabilirim?

SELECT booked_rooms.*, hotels.* FROM 'hotels' 
JOIN 'booked_rooms' ON hotels.hotel_id = booked_rooms.hotel_id
WHERE 
   hotels.city='$city" AND
   (
   booked_rooms.arrival_date BETWEEN '$arrival_date' AND '$departure_date' OR   
   booked_rooms.departure_date BETWEEN '$arrival_date' AND '$departure_date')

'' PHP dizeleri vb için gerekli gibi tablolar etrafında tırnak edin ..

, Dışarıdan güvenilmeyen verileri inşa SQL sorguları koymayın lütfen. Bu Bobby Tablolar sorundur.

Parametreli ifadeleri kullanarak ilgili ayrıntılar için this one gibi bir sayfasına bakınız.

Her şeyden önce, size düzgün CityName alıntı değil ki sizin ilk SQL bir hata var. Sonra resultset dışarı hotel_id getirme yoktur. Ve sonra yanlış yerde ikinci bir döngü var.

Aşağıdaki deneyin:

if( isset($_GET['book']) ) {
    $sql = mysql_query("SELECT hotel_id FROM 'hotels' WHERE city='".mysql_real_escape_string($city)."'") or die(mysql_error());

    $arrival_date = mysql_real_escape_string($arrival_date);
    $departure_date = mysql_real_escape_string($departure_date);
    while( $row = mysql_fetch_assoc($sql) ) {
        $hotel_id = $row['hotel_id'];
        $sql_2 = mysql_query("SELECT *
            FROM `booked_rooms`
            WHERE hotel_id = ".$hotel_id."
                AND (
                    arrival_date BETWEEN '".$arrival_date."' AND '".$departure_date."'
                    OR departure_date BETWEEN '".$arrival_date."' AND '".$departure_date."'
                  )")
        or die(mysql_error());

        while( $row_2 = mysql_fetch_assoc($sql_2) ) {
           print_r($row_2);
        }
    }

}

// $city, $arrival_date and $departure date are values retrieved from the search form

Ben de senin karakterlerini daha cömert olmak tavsiye ederim. Bu okumak için PHP kolaylaştırır.

Ucu AugustLights için teşekkürler.

As i said on the previous post i tried to use JOINS but without any result, and for the query you posted i still have problems with printing fetched data. I guess the problem is in the way i print that data