在WHERE条件下使用BETWEEN

Vij*_*jay 16 php codeigniter

我想要以下功能来选择一个$minvalue和某个住宿之间的住宿$maxvalue.最好的方法是什么?

function gethotels($state_id,$city,$accommodation,$minvalue,$maxvalue,$limit,$pgoffset)
    {
        $this->db->limit($limit, $pgoffset);
        $this->db->order_by("id", "desc");
        $this->db->where('state_id',$state_id);
        $this->db->where('city',$city);

        // This one should become a between selector
        $this->db->where($accommodation,$minvalue); 

        $result_hotels = $this->db->get('hotels');
        return $result_hotels->result();

   }
Run Code Online (Sandbox Code Playgroud)

Mar*_*rco 49

你应该用

$this->db->where('$accommodation >=', minvalue);
$this->db->where('$accommodation <=', maxvalue);
Run Code Online (Sandbox Code Playgroud)

我不确定语法,所以请原谅,如果不正确的话.
无论如何BETWEEN使用> = min && <= max来实现.
这就是我的例子.

编辑:
看看这个链接,我想你可以写:

$this->db->where("$accommodation BETWEEN $minvalue AND $maxvalue");
Run Code Online (Sandbox Code Playgroud)


Muh*_*had 7

在Codeigniter这是一个简单的方法来检查两个日期记录...

$start_date='2016-01-01';
$end_date='2016-01-31';

$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
Run Code Online (Sandbox Code Playgroud)


小智 5

听起来不错但有些问题可能会导致执行此查询:我建议:

$this->db->where( "$accommodation BETWEEN $minvalue AND $maxvalue", NULL, FALSE );
Run Code Online (Sandbox Code Playgroud)

  • 此类型用法可以解决SQL注入安全问题 (2认同)