显示过去7天的结果PHP [英] Display results from past 7 days PHP

查看:146
本文介绍了显示过去7天的结果PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是显示过去30天的文章,但是我拥有的代码不断给我一个mysql_fetch_assoc错误,然后可以追溯到我的查询

What I'm trying to do is display articles from the past 30 days, but the code I have in place keeps giving me a mysql_fetch_assoc error which then traces back to my query

这是代码:

$sql = mysql_query("SELECT * 
                      FROM table 
                     WHERE DATE (datetime) = (CURDATE() - INTERVAL 7 DAY) 
                  ORDER BY ID DESC");

while ($runrows = mysql_fetch_array($sql))

有人可以给我一些有关如何使代码正常工作的指示吗?这是错误

Can someone give me pointers on how to actually make the code work properly? Here is the error

警告:mysql_fetch_array():提供的参数不是有效的MySQL结果资源

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

推荐答案

尝试一下:

$sql = mysql_query("SELECT * FROM table WHERE DATE (datetime) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) ORDER BY ID DESC");

我已经更改了两件事:

  1. 使用DATE_SUB()减去日期.
  2. 使用>=而不是=查找过去一周的所有日期.
  1. Using DATE_SUB() to subtract date.
  2. Using >= instead of just = to find all dates in the past week.

这篇关于显示过去7天的结果PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆