0

我正在制作一个拍卖类型的网站,您可以想象它应该在产品过期时将其下架。过期只是存储为 MySQL 数据类型 DATE。

所以check_items.php:

<?php
function check_items()
{
$con = mysql_connect('localhost','heh','heh');
mysql_select_db('heh_db',$con);
$q = mysql_query("select last_check from ran_last",$con) or die("Check ran_last 1");
$r = mysql_fetch_assoc($q);
//if((time()-strtotime($r['last_check'])) >(60*60*17))//check only once every 17 hours
if(true)
{
    $q2 = mysql_query("select * from Item where expired=0");
    $remove = array(); $count=0;
    while($row = mysql_fetch_assoc($q2))
    {
        if(strtotime($row['time_expire'])<time())
        {
            echo("strtotime: ".strtotime($row['time_expire'])." time: ".time());
            $remove[$count] = $row['ItemID'];
            $count++;
        }
    }
    mysql_free_result($q2);
    foreach($remove as $next)
    {
        echo($next);
        $q3 = mysql_query(sprintf("select * from Item where ItemID='%s'",$next)) or die("check items outer query foreach");
        $r3 = mysql_fetch_assoc($q3);
        $q4 = mysql_query(sprintf("update Item set expired='1' where ItemID='%s'",$r3['ItemID']));
        if(isset($r3['bidderID']))
        {
            $f1 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['bidderID'],
                $r3['item_name'],
                $r3['ItemID'],
                "BUY",
                sprintf("You have won the bidding for this item. Contact the <a href=\"pm.php?ID=%s&&expired_item=%s\">seller</a> for details",
                    $r3['userID'],
                    $r3['ItemID'])
                ),$con
            );
            $f2 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['userID'],
                $r3['item_name'],
                $r3['ItemID'],
                "SELL",
                sprintf("<a href=\"pm.php?ID=%s&&expired_item=%s\">User</a> has won the bidding for your item. You are encouraged to contact each other",$r3['bidderID'],
                    $r3['ItemID'])
                ),$con
            );
        }
        else
        {
            $f1 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['userID'],
                $r3['item_name'],
                $r3['ItemID'],
                "SELL",
                sprintf("Unfortunately no one bid on your item. You can view expired items from your userpage and re-upload",
                    $r3['userID'])
                ),$con
            );
        }
        mysql_free_result($q3);
    }
    $done = mysql_query("insert into ran_last values()");
}
mysql_free_result($q);
}
?>

该脚本功能齐全,存储上次更新的时间,并且应该每 17 小时运行一次。现在,只要调用该函数,它就会运行。基本上每当我列出产品时,它都会自动被除名。

4

1 回答 1

1

呃,你为什么要这样?你可以在 PHP 中进行过滤,省去很多麻烦:

SELECT ItemID
FROM Item
WHERE (expired = 0) AND (time_expire < (SELECT last_check FROM last_ran))

另外,当您进行真正的到期检查时,您甚至没有检查此值:

    if(strtotime($row['time_expire'])<time())
                                      ^^^^^^--- shouldn't this be $r['last_check']?
于 2012-12-13T18:10:25.567 回答