user3221406 user3221406 - 1 year ago 58
MySQL Question

PHP Mysql Query with Time Interval

I have a php that extracts information from a mysql database just fine, however when I tried to add the functionality to check for data depending on an interval I am not getting any data from the database, just replies with an empty result. When I run it in Phpmyadmin it works just fine. Here's the code:

$result = mysql_query("
WHERE (SensorTag='$requestedSensor' and TimeEntered<'$end'
and TimeEntered>'$start');
") or die(mysql_error());

start and end are of the form: 2014-01-22 15:36:37 just like my timestamped column in the database.

What am I doing wrong here? I assume something is wrong with the query but I can't figure out what.

Answer Source

Same can be achieved using BETWEEN clause as follows:

$result = mysql_query("SELECT * FROM SensorLog 
WHERE (SensorTag='$requestedSensor' and TimeEntered BETWEEN '$end' AND '$start')") or die(mysql_error());