user3221406 user3221406 - 5 months ago 10
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("
SELECT * FROM SensorLog
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

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());
Comments