Drupal 7: can't get SQL query to work that works outside of Drupal I have a SQL query that works just fine on it's own as follows:

SELECT timestamp FROM accesslog
WHERE uid = 1 
AND path = 'mypath' 
AND timestamp &gt; UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 4 WEEK));


I've tried two methods, and the first one returns all values for the user from the beginning of time, and the second results in a PDOException.

Method 1:

$ts = 'UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL '.$interval.'))';
$query = db_select('accesslog', 'a');
$query-&gt;addField('a', 'uid');
$query-&gt;addField('a', 'path');
$query-&gt;addField('a', 'timestamp');
$query-&gt;condition('a.uid', $uid);
$query-&gt;condition('a.path', $path);
$query-&gt;condition('a.timestamp', $ts, '&gt;');
$result = $query-&gt;execute();


Method 2:

$result = db_query("
SELECT timestamp 
FROM accesslog
WHERE uid = :uid 
AND path = :path
AND timestamp &gt; UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :interval))",
array(':uid'=&gt;$uid,
      ':path'=&gt;$path,
      ':interval'=&gt;$interval))-&gt;fetch();    


I'm guessing the issue is with the timestamp > part, but I'm just not sure how to get that part done. 

Can someone point me in the right direction?

Thanks
Instead of the above one try the following query,

$date = date_create();
date_modify($date, '-4 week');
$interval =  $date-&gt;format('U');

$result = db_query("
SELECT timestamp 
FROM accesslog
WHERE uid = :uid 
AND path = :path
AND timestamp &gt; :interval",
array(':uid'=&gt;$uid,
      ':path'=&gt;$path,
      ':interval'=&gt;$interval))-&gt;fetchAll();