-1

Right now I have the following query:

$sth = $dbh->prepare('SELECT * from training ORDER BY startDate ASC LIMIT 4');

Then, I process it further like this:

if($row['endDate'] > $today && $row['status'] != '2')

How can I combine the if statement into the db query? I'm not sure how to check the endDate and compare it to today's date in the query.

Mike
  • 1
  • 1
  • 1

2 Answers2

0

Just:

"SELECT * from training 
 WHERE endDate > '$today' 
 AND status != 2 
 ORDER BY startDate ASC LIMIT 4"

works if endDate is a DATE orSTRING field. Or $today is like '2015-15-01' eg date('Y-m-d');

ob_start
  • 276
  • 1
  • 3
0

This will give you a totally different set of results; since you were limiting your query and then filtering it, it would be quite probable to get no valid results. Since you are now filtering the results of the entire table, there are many more potential matches to choose from.

Also you specifically mentioned "today's date", so that it what this is comparing against.

$sth = $dbh->prepare(
    "SELECT * from training 
    WHERE endDate > CURDATE() AND status != 2 
    ORDER BY startDate ASC LIMIT 4"
);
miken32
  • 42,008
  • 16
  • 111
  • 154