Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter中的SQL where条件_Php_Sql_Codeigniter - Fatal编程技术网

Php Codeigniter中的SQL where条件

Php Codeigniter中的SQL where条件,php,sql,codeigniter,Php,Sql,Codeigniter,我想检查是否有预订。我有入住日期、退房日期和房间号。我尝试了下面的查询,但它也给出了所有不属于该房间id的数据。我做错了什么 $array = array('checkin_date' => $checkin, 'checkout_date' => $checkin); $array1 = array('checkin_date >' => $checkin, 'checkout_date <' => $checkin); $array2 = array('c

我想检查是否有预订。我有入住日期、退房日期和房间号。我尝试了下面的查询,但它也给出了所有不属于该房间id的数据。我做错了什么

$array = array('checkin_date' => $checkin, 'checkout_date' => $checkin);
$array1 = array('checkin_date >' => $checkin, 'checkout_date <' => $checkin);
$array2 = array('checkin_date <' => $checkin, 'checkout_date >' => $checkin);
$array3 = array('checkin_date <' => $checkin, 'checkout_date <' => $checkin);
$array4 = array('checkin_date >=' => $checkin, 'checkout_date <=' => $checkin);
$array5 = array('checkin_date >=' => $checkout, 'checkout_date <=' => $checkout);

$this->db->select('*');
$this->db->from('igc_room_booking');
$this->db->where('delete_status', 0);
$this->db->where('cancel_status', 0);
$this->db->where('room_id', $val->room_id);
$this->db->where($array);
$this->db->or_where($array2);
$this->db->or_where($array3);
$this->db->or_where($array);
$this->db->or_where($array4);
$this->db->or_where($array5);
$bookingss = $this->db->get()->result();
$array=array('checkin\u date'=>$checkin,'checkout\u date'=>$checkin);
$array1=array('checkin\u date>'=>$checkin,'checkout\u date'=>$checkin);

$array3=array('checkin\u date='=>$checkin,'checkout\u date='=>$checkout,'checkout\u date我认为问题在于你的查询是错误的……你应该删除它

$this->db->select(*)和$this->db->from('igc\u room\u booking')


在所有where子句之后,您应该使用

$this->db->get('igc_room_booking')


现在我没有考虑你的逻辑-因为Simos Fasouliotis在评论中说得够多了-所以你在这里得到了一个正确的想法,在句法上

但是,如果使用
get\u compiled\u select
编译查询,则会得到以下结果:

SELECT *
FROM `igc_room_booking`
WHERE `delete_status` =0
AND `cancel_status` =0
AND `room_id` = 12
AND `checkin_date` = '2010-09-01'
AND `checkout_date` = '2010-09-01'
OR `checkin_date` < '2010-09-01'
OR `checkout_date` > '2010-09-01'
OR `checkin_date` < '2010-09-01'
OR `checkout_date` < '2010-09-01'
OR `checkin_date` = '2010-09-01'
OR `checkout_date` = '2010-09-01'
OR `checkin_date` >= '2010-09-01'
OR `checkout_date` <= '2010-09-01'
OR `checkin_date` >= '2010-10-01'
OR `checkout_date` <= '2010-10-01'
但我认为这没有任何意义,你应该清楚地考虑你需要的日期范围…

试试这个:

$array = "('$checkindate' BETWEEN `checkin_date` AND `checkout_date`) OR  ('$checkoutdate' BETWEEN `checkin_date` AND `checkout_date`) OR (`checkin_date` > '$checkindate' AND `checkout_date` < '$checkoutdate')";
    $this->db->select('*');
    $this->db->from('igc_room_booking');
    $this->db->where('delete_status', 0);
    $this->db->where('cancel_status', 0);
    $this->db->where('room_id', $val->room_id);
    $this->db->where($array);
    $bookingss = $this->db->get()->result();
$array=“(`checkin\u date`和`checkout\u date`之间的'$checkindate')或(`checkin\u date`和`checkout\u date`之间的'$checkoutdate'))或(`checkin\u date`>'$checkindate'和`checkout\u date`<'$checkoutdate');
$this->db->select('*');
$this->db->from('igc_room_booking');
$this->db->where('delete_status',0);
$this->db->where('cancel_status',0);
$this->db->where('room\u id',$val->room\u id);
$this->db->where($array);
$bookingss=$this->db->get()->result();

什么是
$val
?您应该回显您的查询,并在phpMyAdmin上检查您使用的SQL版本是什么?您正在将事情过度复杂化,可能在1种情况下,介于两者之间就足够了。
$array = "('$checkindate' BETWEEN `checkin_date` AND `checkout_date`) OR  ('$checkoutdate' BETWEEN `checkin_date` AND `checkout_date`) OR (`checkin_date` > '$checkindate' AND `checkout_date` < '$checkoutdate')";
    $this->db->select('*');
    $this->db->from('igc_room_booking');
    $this->db->where('delete_status', 0);
    $this->db->where('cancel_status', 0);
    $this->db->where('room_id', $val->room_id);
    $this->db->where($array);
    $bookingss = $this->db->get()->result();