Robert Coroianu
Robert Coroianu

Reputation: 426

PHP MYSQL SELECT BETWEEN DATE'S

Hello i trying to select date from mysql between date's with this code:

if (empty($_GET['date-range1'])) { 
    $sql=mysql_query("SELECT * FROM sohy_raports ORDER BY ".$_GET['sort']." ".$_GET['ad']."");

} else {
$sql=mysql_query("SELECT * FROM sohy_raports WHERE date BETWEEN ".$_GET['date-range1']." AND ".$_GET['date-range2']." ORDER BY ".$_GET['sort']." ".$_GET['ad']."");
}

but with this code i can't select between date's only between id's. It can be from date format error Y-mm-dd ?

Thanks

Upvotes: 0

Views: 9110

Answers (1)

Borniet
Borniet

Reputation: 3546

Instead of

$sql=mysql_query("SELECT * FROM sohy_raports WHERE date BETWEEN ".$_GET['date-range1']." AND ".$_GET['date-range2']." ORDER BY ".$_GET['sort']." ".$_GET['ad']."");

do

$query = "SELECT * FROM sohy_raports WHERE date BETWEEN ".$_GET['date-range1']." AND ".$_GET['date-range2']." ORDER BY ".$_GET['sort']." ".$_GET['ad'];
echo $query;
$sql=mysql_query($query);

This will print out the query you are sending to your database, and will clear up alot of what might go wrong, and what exactly the date-format is you are using.

On the side, but no less important: - Don't use the mysql_* functions anymore, they are deprecated and unsafe. Switch to mysqli_* or PDO instead. - Never just use your GET variable (or POST) in your query, make sure you sanitize them first to prevent SQL injections.

Upvotes: 2

Related Questions