homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
php, mysql and pulling by day
scrupply




msg:4308390
 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,

 

eelixduppy




msg:4308467
 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




msg:4308468
 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




msg:4308492
 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




msg:4308816
 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




msg:4308833
 5:17 pm on May 6, 2011 (gmt 0)

Well that should do it. :-)

scrupply




msg:4308837
 5:27 pm on May 6, 2011 (gmt 0)

What should do it? I think that I am lost.

rocknbil




msg:4309909
 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>";
}

scrupply




msg:4314963
 10:39 pm on May 19, 2011 (gmt 0)

Thank you, I think that this did it.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved