问题
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