1

I am working on PHP and MySQL, I want to count result that are generated today and yesterday.

Here is screenshot of my phpmyadmin and table is results. In date_generate column I have used TIMESTAMP

table

I have this query which count total results

<?php

   $query = $db->query("SELECT * FROM `results`");
   $count = $query->rowCount();
   echo $count;
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51

2 Answers2

1

You need to use group by to get the records created based on date_genrate. So, your query will be something like Considering date_generate as timestammp

SELECT DATE(FROM_UNIXTIME(date_generate)) AS date_g, COUNT(id) AS NumPosts
 FROM   results
 WHERE date_generate BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 day) AND CURDATE()
 GROUP BY date_g
 ORDER BY date_g
Naincy
  • 2,953
  • 1
  • 12
  • 21
  • i tried this but still this showing output is 1 here is query i tried 'query("SELECT DATE(FROM_UNIXTIME(date_generate)) AS date_g, COUNT(id) AS NumPosts FROM results WHERE date_generate BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 day) AND CURDATE() GROUP BY date_g ORDER BY date_g "); echo $query->execute(); ?> ' – Rohaan Ahmed Feb 21 '17 at 10:14
  • get row_count.... execute will return only query executed successfully or not....try running query directly in phpmyadmin and see result... – Naincy Feb 21 '17 at 10:19
0

Replace your query :

 $query = $db->query("SELECT * FROM `results` where date_generate BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 day) AND CURDATE()");
$query->execute(); 
echo $count = $query->rowCount();
Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23