Good time! I know there are many similar questions here, I looked through them but did not find a suitable option - I just tried it did not work. In the comments to neksolski questions asked a question but also failed to figure it out - by this, do not hit with your feet) the essence is

$datenow = date("Ymd"); $tmpact = mysql_query("SELECT * FROM actions WHERE fdate <= '$datenow' AND tohda <= '$datenow'"); 

the fdate and tohda fields contain strings with the following dates in the database:

fdate - 2016-11-12 and 2016-09-02 and tohda - 2017-08-14 and 2018-08-01 respectively

but the query returns 0 rows, even if I remove the WHERE conditions completely, I just make a selection, then everything is selected, the query passes, the fields in BD contain dates (fdate and tohda) of data type DATE. I also tried STR_TO_DATE - there is no effect either. Help please maybe I just don’t see any obvious things and even this doesn’t work like that when directly

$ tmpact = mysql_query ("SELECT * FROM actions WHERE fdate <= '2016-04-01' AND tohda <= '2016-04-01'");

  • Try to still see what date you actually have in the database. - Ipatiev

1 answer 1

We learn to debug SQL.
To begin, we write

 $datenow = date("Ymd"); $sql = "SELECT * FROM actions WHERE fdate <= '$datenow' AND tohda <= '$datenow'"; die($sql); 

And we look at the request received, is everything normal with it? If yes, then go to the mysql console (or, forgive my God, pokapemayadmin) and run the query there.

If it finds strings, then we check which database we are connecting to in PHP.
If it does not, then we check our fantasies about what data is actually recorded in the database, for example:

 SELECT fdate, '$datenow', fdate <= '$datenow' as result FROM actions 

and see what output.

Separately, it should be noted that instead of the idiotic mysql_query and the variables being pushed in directly into the query, PDO and placeholders should be used.

 $stmt = $pdo->prepare("SELECT * FROM actions WHERE fdate <= ? AND tohda <= ?"); $stmt->execute([$datenow,$datenow]); $data = $stmt->fetchAll(); 
  • $stmt->execute([$datenow, $datenow]); ? - E_p
  • @ Ipatiev damn even with a query on a line without variables so SELECT fdate, '2016-08-08', fdate <= '2016-08-08' AS result FROM hystactionsda zero lines gives out! ( - dantelol
  • @dantelol It is necessary to execute the query that I cited above, the query that shows next the date, the value from the field and the result of the comparison. Although it is clear that the problem is in the data in the database - Ipatyev
  • @ Ipatiev to the word here are the screenshots of the fields in the database [Fields in the database] () - dantelol
  • @ Ipatiev screen request returned the request like this = PMA - as the site is doing \ - dantelol