CodeIgniter Forums

Full Version: Problem with database query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 2 tables in database. 
I need to get from database all rooms in which there is no reservation in the hours and date given by me.

Quote:id int(11)
name varchar(15)

Quote:id int(11)
room_id int(11)
date date
time_start time
time_finish time

My function: 

PHP Code:
public function get_free_rooms() {
   $date $this->input->post('date'true);
   $time_start $this->input->post('time_start'true);
   $time_finish $this->input->post('time_finish'true);

   $query $this->db->join('reservations',' = reservations.room_id','left')
       ->where(' IS NULL'nullfalse)
   $query $query->result();

   echo json_encode($query);

My reservations table is empty now. When I give data:

Quote:$date = 2019-05-29, $time_start = 10:00:00, $time_finish = 13:00:00

result of query is an empty array and should return all rooms because there is no booking.
After you run your query did you use

PHP Code:
$temp $this->db->last_query();


To see what your query looks like?

What does your query look like when it is executed? Also, why don't you have a from and to date? Is the user only allowed to have the room for 1 day?
@InsiteFX, $temp return this:

Quote:SELECT * FROM `rooms` LEFT JOIN `reservations` ON `rooms`.`id` = `reservations`.`room_id` and `reservations`.`date` = '2019-05-29' and `reservations`.`time_start`<='10:00' and `reservations`.`time_finish`>='13:00' WHERE IS NULL

@php_rocs, yes. Reservations are only for one day.