codeigniter get_where with mysql functions in check values

試著忘記壹切 提交于 2020-03-26 02:46:51

问题


Just a question about the get_where function of codeigniter Active Records. I wanted to get a query which should be like this:

Select * from foo 
where date = STR_TO_DATE('$month/$day/$year','%m/%d/%Y') 
  and time = STR_TO_DATE('$hour:$minute$ampm', '%l:%i%p')

Now I created a function in my model which appears like this:

public function validate_if_existing($month,$day,$year,$hour,$minute,$ampm)
{
    $event_date = "STR_TO_DATE('$month/$day/$year','%m/%d/%Y')";
    $event_time = "STR_TO_DATE('$hour:$minute$ampm', '%l:%i%p')";

    $this->db->flush_cache();
    $query = $this->db->get_where('foo',array('event_date' => $event_date,'event_time'=> $event_time));

    return $query->num_rows();
}

But it returns 0 rows where I expected it to be 1. I tried to check the $event_date and $event_time contents and it just appears exactly as I expected. Like this:

STR_TO_DATE('01/01/2012','%m/%d/%Y')

Am I doing this wrong? Anyway to solve this out?


回答1:


got it already! I just manually added the filters using the $this->db->where() like this:

$this->db->where('event_date',"STR_TO_DATE('$month/$day/$year','%m/%d/%Y')", FALSE);

works just like in $this->db->select(). The $this->db->get_where() is just a shortcut form. good to use when making a non complex query. Thanks to air4x for giving me some tips. :)



来源:https://stackoverflow.com/questions/13110913/codeigniter-get-where-with-mysql-functions-in-check-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!