1

I have a table set up with these types Columns:

Date_Time datetime PK 
ppm2      int 
ppm10     int 
aqi       int

the query I am using is

SELECT * FROM aqi_data where Date_Time = CURDATE()

This returns 0 records

<?php $chartQuery = "SELECT * FROM aqi_data where Date_Time = CURDATE()"; 
$chartQueryRecords = mysqli_query($con, $chartQuery); 
while($row = mysqli_fetch_assoc($chartQueryRecords)){ 
  echo "'".$row['Date_Time']."',".$row['aqi']. 
       ",".$row['ppm2'].",".$row['ppm10']."],"; 
} ?>

Can anyone help me with this, please?

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
blaineca
  • 11
  • 2
  • Please share some sample codes, so someone can relate to the real scenario & can help you. https://stackoverflow.com/help/minimal-reproducible-example – Raju Ahmed Sep 18 '22 at 04:02
  • are you passing date with time ? Date time column contains time also...and you have to just compare date. After removing time parameter, check the format also. – Amit Verma Sep 18 '22 at 04:22
  • 1
    Does this answer your question? [How to select rows that have current day's timestamp?](https://stackoverflow.com/questions/14769026/how-to-select-rows-that-have-current-days-timestamp) – kmoser Sep 18 '22 at 05:35

1 Answers1

0

You have stored DateTime and you are comparing with date. One way is to cast the data as date

<?php 
$chartQuery = "SELECT * FROM aqi_data where CAST(Date_Time AS DATE) = CURDATE()"; 
$chartQueryRecords = mysqli_query($con, $chartQuery); 
while($row = mysqli_fetch_assoc($chartQueryRecords)){ 
  echo "'".$row['Date_Time']."',".$row['aqi']. 
       ",".$row['ppm2'].",".$row['ppm10']."],"; 
} ?>

Another way, which will be faster is to compare Date_Time with a range between CURDATE and CURDATE + 1DAY.

"SELECT * FROM aqi_data where Date_Time >= CURDATE() and Date_Time < CURDATE()+1";

Not Tested !

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41