dongtu0088 2015-09-16 18:28
浏览 49
已采纳

试图让php查询在一天中发生一次

I want this Code to do query just once in a day means after it happened the code will be hidden or disabled some how, is that possible ? thanks :)

$time = date("G");
if($time >= 12 && (isset($_SESSION['UserName']))){
    mysql_query("Update users set diamonds = diamonds+1 where username = 'NaiTreNo'");
}
  • 写回答

2条回答 默认 最新

  • doushi3202 2015-09-16 19:58
    关注

    Of course a cronjob is a start, but even cronjobs might fail or you might have multiple machines for the same database.

    Extend the table with a column last_daily_diamond datetime, which you set when the user gets its diamond, so:

    update users set diamonds=diamonds+1, last_daily_diamond = now()
    where username='flaschenpost' and last_daily_diamond < now() - interval 24 hour
    ;
    

    If you mean calendar days and not 24-hour-days, then use not datetime, but date column for last_daily_diamond.

    update users set diamonds=diamonds+1, last_daily_diamond = curdate()
    where username='flaschenpost' and last_daily_diamond < 
    

    curdate() ;

    If user comes after 10 days and should get 10 diamonds (this decision is up to your customer/boss/yourself!), it gets

    update users set diamonds=diamonds+1, last_daily_diamond = last_daily_diamond + interval 1 day
    where username='flaschenpost' and last_daily_diamond < 
    

    curdate() ;

    (if the query itself is send often enough)

    or a bit more complicated (not tested yet)

    update users set diamonds=diamonds+
    (@given := round((unix_timestamp(curdate()) - unix_timestamp(last_daily_diamond))/(24*60*60))), 
    last_daily_diamond = curdate() + interval @given days
    where username='flaschenpost' and last_daily_diamond < 
    

    curdate() ;

    If you can not extend the users table, you could also create an extra table for this and join to users table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)