Khan Khan - 3 months ago 11
PHP Question

How to select date range in mysql?

i want to select a date range in mysql.

$dateFrom='2016-01-01';
$dateTo='2016-01-02';


NOT WORKING

query ="SELECT ".$name.",timestamp FROM people where age=".$age." AND timestamp>=".$dateFrom." AND timestamp<.$dateTo";


WORKING

query ="SELECT ".$name.",timestamp FROM people where age=".$age." AND timestamp>='2016-01-01' AND timestamp<'2016-01-02';

Answer

You will need to put ' single quotes arround dates variable.

query ="SELECT ".$name.",timestamp FROM people where age=".$age." AND timestamp>='".$dateFrom."' AND timestamp<'".$dateTo."';

Make sure you're open to SQL injection attacks, you should better use Prepared statements to avoid the same.