-1

我正在编写一个函数,该函数检索从当前日期开始的指定日期范围内的四个特定列之一中的所有条目。IE。这些列包含到期日期,我希望能够显示从当前日期起 30、60、90 天内到期的条目。我在正确处理它时遇到了一些麻烦,您可以提供任何帮助,我将不胜感激。另外请不要给我准备好的语句和 PDO 讲座,我很清楚它和它的必要性,但这不是用于外部消耗,所以 sql 注入不是问题,过度复杂化对我来说会适得其反.

这是我到目前为止所拥有的。

// list_entries_set function lists all entries expiring in the next $days
function list_entries($days) {
    if(!$days) $days = "365";

    $today = date('Y-m-d');
    $end_day = strtotime(date("Y-m-d", strtotime($end_day)) . " + '.$days.'day");

    db_connect();

    $orderby = $_GET["orderby"];
    $sql = mysql_query("SELECT * FROM table WHERE column BETWEEN ".$today." AND ".$end_day."  ORDER BY ".$orderby);

    while($row = mysql_fetch_array($sql)) {
        echo "table outputs : purged because it's irrelevant and lengthy";
    }

    echo "</table>";
    db_disconnect();
}
4

2 回答 2

1
SELECT * FROM table WHERE column BETWEEN NOW() AND NOW() + INTERVAL :duration DAY LIMIT ORDER BY :order;

使用准备好的支持语句的引擎来解析占位符。ext/mysql不再是一种选择。

于 2013-02-10T03:00:34.687 回答
0

从当前日期起 30、60 或 90 天内过期的条目的 sql 将是这样的;

select item
, case when datediff(expiry_date, current_date) <= 30 then 'within 30'
when datediff(expiry_date, current_date) <= 60 then 'within 60'
else 'within 90'
end whatever_you_want_to_call_this
from your_tables_joining_if_necessary
where expiry_date >= current_date
and expiry_date < date_add(current_date, 91, days)

mysql 的语法可能不正确。我不使用它,只是在看手册。

于 2013-02-10T03:57:10.107 回答