PHP为什么会无休止地循环?

时间:2015-02-15 21:24:35

标签: php mysql

我有以下函数,应该在两个日期之间循环,将信息添加到mysql数据库。出于某种原因,它无休止地循环,并从1970年开始插入行

function blackout($start, $end, $class, $camper, $res) {
    $date=date ("Ymd", strtotime(strtotime($start)));
    while (strtotime($start) <= strtotime($end)) {
        mysql_query("INSERT INTO availability VALUES ('','$date','$class','$camper','$res')");

        $date = date ("Ymd", strtotime("+1 day", strtotime($date)));

    }
}

这就是我所说的:

blackout(20150303,20150310,"classone",4,5);

所以它应该在数据库中输入7行,但是如果我不关闭它就会产生数十万行。

2 个答案:

答案 0 :(得分:0)

您的循环不断更新$date,但条件基于$start$end之间的关系。由于它们未被修改,循环将永远继续。

据推测,您打算将$date$end进行比较:

while (strtotime($date) <= strtotime($end)) {

答案 1 :(得分:0)

在while条件

中使用变量$ date而不是$ start
相关问题