I have a mysql database with over a million records and need to see how many records exist between specific dates.
This is my code and always get one row.
I have validated that over the period in my code there are over 500 records.
What am I doing wrong?
$start_date = "1990-01-01";
$end_date = "1990-02-31";
echo "Start date is : " . $start_date . "<br>";
echo "End date is : " . $end_date . "<br>";
$sql = "SELECT COUNT(*) FROM " . $rgdtable . " WHERE `DATE-REGISTERED` BETWEEN " . $start_date . " AND " . $end_date;
echo "Sql string is : " . $sql . "<br>";
if ($result=mysqli_query($conn,$sql)) {
$rowcount=mysqli_num_rows($result);
echo "The total number of rows are: ".$rowcount;
}
2
Answers
In your code – $end_date = "1990-02-31"; Is this even valid? February doesn’t have this? Shouldn’t it be $end_date = "1990-02-28" or "1990-03-01". If needed you can always do end_date minus 1 given that the datatype is Date
you can also set the end_date 1 day higher und subcract 1 day in SQL.
So you everytime this last day of the month before
like this: