Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.