I am trying to pull data by day for last seven days from MySQL. Is this possible? If so, how would I do it?
Thank you,
eelixduppy
8:32 pm on May 5, 2011 (gmt 0)
Just to clarify, do you mean from a single table/relation or from an entire MySQL database (e.g., for backup purposes)?
scrupply
8:43 pm on May 5, 2011 (gmt 0)
The information would come through a single call and then would display out on a page.
rocknbil
9:59 pm on May 5, 2011 (gmt 0)
Welcome aboard . . . you have a field with the data type of date or datetime . . .then
select * from table where datefield >= date_sub(curdate(), interval 7 day);
If you don't have a field with a date/timestamp, you should. :-) Create one.
Alter table tablename add datefield datetime not null default '0000-00-00 00:00:00';
Try to avoid naming fields the same name as reserved words . . . date, datetime, insert, etc.
scrupply
4:53 pm on May 6, 2011 (gmt 0)
I have a field that is datetime (mysql) and I am trying to pull the data from that table that is within the last 7 days (PHP).
rocknbil
5:17 pm on May 6, 2011 (gmt 0)
Well that should do it. :-)
scrupply
5:27 pm on May 6, 2011 (gmt 0)
What should do it? I think that I am lost.
rocknbil
4:32 pm on May 9, 2011 (gmt 0)
What should do it? I think that I am lost.
I have a field that is datetime (mysql) and I am trying to pull the data from that table that is within the last 7 days (PHP).
Substitute "datefield" for whatever that field is named and "table" for the name of the table.
select * from table where datefield >= date_sub(curdate(), interval 7 day);
In PHP, that would be something like
$query = "select * from table where datefield >= date_sub(curdate(), interval 7 day)"; $result = mysql_query($query) or die("Cannot get last 7 days: " . mysql_error()); while ($row = mysql_fetch_array($result)) { // Row data is stored in $row, whatever the field names are . . . . echo "<p>ID: " . $row['id'] . " date: " . $row['datefield'] . "</p>"; }