Jump to content

Retriving Records From Past Dates


Bopo

Recommended Posts

Hi

 

Hi

 

Basically I want to write a query which returns records from the past 7 days, I have a DATE field which stores dates in the following format 2009-04-08 (standard format), heres the query below

 

<?php if($getdate == "7day") {
$day = 7;
$sql = "SELECT * FROM comments WHERE date(CURDATE(), INTERVAL '$day' DAY) <= $todaysdate";  //date = column field in table

echo $sql;
}?>

 

Here's what is output from the echo

 

SELECT * FROM comments WHERE date(CURDATE(), INTERVAL '7' DAY) <= 04.08.09

 

Error:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in

Link to comment
https://forums.phpfreaks.com/topic/153152-retriving-records-from-past-dates/
Share on other sites

Thanks for the reply, I thought that would definitely fix the problem, however I'm still getting the same error, could it be something to do with my column being called date? here is the new sql output

 

SELECT * FROM comments WHERE date(CURDATE(), INTERVAL '7' DAY) <= '2009/04/08'

Thanks for the suggestions, I have changed the date format to the correct format, also I implemented the mysql_error function for my query, here is what I got back:

 

SELECT * FROM comments WHERE date(CURDATE(), INTERVAL '7' DAY) <= '2009-04-08'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' INTERVAL '7' DAY) <= '2009-04-08'' at line 1

 

 

Nah the back ticks didn't change anyway, I'm using MySQL client version: 5.0.67

 

As a last resort, would using a between clause work between two dates, like:

 

select * from comment where date between $todaysdate and $7daysago

 

How would you minus 7 days from today's date variable though?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.