web_newbie
web_newbie

Reputation: 115

Convert mysql query to CodeIgniter $query->row

How to convert this query to CodeIgniter?

$date = '28-05-2019';
$time_start = '10:00';
$time_end = '19:00';

select courtrooms.* from courtrooms
left join reservations 
on courtrooms.id = reservations.courtroom_id
and reservations.date = '$date' and reservations.time_start < '$time_start' and reservations.time_end > '$time_end'
where reservations.id is null

Upvotes: 0

Views: 24

Answers (1)

dr.dev
dr.dev

Reputation: 66

There are actually several ways to write queries in Codeigniter, the following syntaxis is the most understandable from my point of view if you are just starting to write queries using this framework.

Please see Query Builder Class

$this->db->select('*');
$this->db->from('courtrooms');
$this->db->join('reservations','courtrooms.id = reservations.courtroom_id','left');
$this->db->where('reservations.id IS NULL', null, false);
$this->db->where('reservations.date', $date); 
$this->db->where('reservations.time_start<', $time_start);
$this->db->where('reservations.time_end>', $time_end);
$query = $this->db->get();

$query = $query->result();

The above code is the same than this one:

$query = $this->db->join('reservations','courtrooms.id = reservations.courtroom_id','left')
                  ->where('reservations.id IS NULL', null, false)
                  ->where('reservations.date', $date)
                  ->where('reservations.time_start<', $time_start)
                  ->where('reservations.time_end>', $time_end)
                  ->get('courtrooms');
$query = $query->result();

Upvotes: 1

Related Questions