Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: open

Message Too Old, No Replies

php, mysql and pulling by day

     
5:57 pm on May 5, 2011 (gmt 0)



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,
8:32 pm on May 5, 2011 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Just to clarify, do you mean from a single table/relation or from an entire MySQL database (e.g., for backup purposes)?
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.
9:59 pm on May 5, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
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).
5:17 pm on May 6, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well that should do it. :-)
5:27 pm on May 6, 2011 (gmt 0)



What should do it? I think that I am lost.
4:32 pm on May 9, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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>";
}
10:39 pm on May 19, 2011 (gmt 0)



Thank you, I think that this did it.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month