简体   繁体   English

将日期传递到where子句时出错

[英]error when passing a date to where clause

So this is odd and hopefully there is a simple answer. 因此,这很奇怪,希望有一个简单的答案。 I have a SQL query that used a date as the first parameter in a where() clause. 我有一个SQL查询,该查询使用日期作为where()子句中的第一个参数。 But for some reason CodeIginter appears to be trying to add in extra escape characters, 但是出于某种原因,CodeIginter似乎试图添加额外的转义字符,

Here is my code 这是我的代码

        $this->db->select("*");
        $this->db->from("equip_reserve");
        $this->db->where('equip_list_id = '.$equip_list_id);
        $this->db->where('"'. date("Y-m-d H:i",strtotime($startdate)) .'" between date and returndate');

Here is the result: 结果如下:

SELECT * FROM 'equip_reserve' WHERE 'equip_list_id' = 5 AND "2016-02-29 '14:20'" between 'date' and 'returndate'

As you can see the date after the AND clause is causing this statement to fail. 如您所见,AND子句导致该语句失败的日期。

Any suggestions on how to fix it? 关于如何解决它的任何建议?

I ended up fixing it by just passing the SQL directly to CodeIgniter instead of using their helper functions. 我最终通过仅将SQL直接传递给CodeIgniter而不是使用其辅助函数来解决了该问题。 In case anyone ever has this problem. 万一有人遇到这个问题。

$st = date("Y-m-d H:i",strtotime($startdate));
$sql  =  "SELECT * FROM `equip_reserve` WHERE  ? between `date` AND `returndate` and `equip_list_id` = ?"; 

$query = $this->db->query(
   $sql, 
   array($star,$equip_list_id)

); );

return $query->result(); 返回$ query-> result();

Fixed! 固定!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM