My Sql DATE_SUB() and codeigniter Active records issue?

codeigniter, datetime, mysql, php

Solution

Try doing something like this instead:

$this->db->where("added_datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR)", NULL, FALSE);
$query = $this->db->get('tablename');

`$this->db->get_where();` doesn't provide the function to unescaping your where clause.

Problem

i am using codeigniter active records to execute a simple mysql query to select data in last one hour. so my query is: ``` SELECT * FROM tablename WHERE added_datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR) ``` and my codeigniter code to form this query is: ``` $data=array( 'added_datetime >='=>'DATE_SUB(NOW(),INTERVAL 1 HOUR)', ); $query=$this->db->get_where('tablename',$data); ``` now the issue is codeigniter adds a single quotes around the DATE_SUB function and due to this the query not works on mysql server. codeigniter produces: ``` SELECT * FROM (`tablename`) WHERE `added_datetime` >= 'DATE_SUB(NOW(),INTERVAL 1 HOUR)' ``` i also tried by adding `FALSE` as 3rd parameter in `get_where` but stil not worked ``` $query=$this->db->get_where('tablename',$data,FALSE); ``` it also produced the same query as above. So please suggest me how to solve this issue. -Thanks

Original source