在一定时间后自动更新行 [英] automatically update row after certain time
问题描述
现在,我想在一段时间后自动更改广告表格中的一行。
例如:假设我的用户上传/发布了广告,在发布广告之前,如果他想让广告保持显示8天或16天,他会选择。
我应该用cron检查一下,还是有更好的方法吗?
可见被称为活动数据库, - tinyint 1或0。
/ p>
您有两个选项可以根据他们选择的时间范围来管理。
-
存储广告的到期时间,包括expiration> NOW();
-
使用事件来管理此事件。
/ li>
方法1可能需要管理员定期清理广告。
方法2 - 您不能在过程中创建事件(上次我检查),但它们允许您设置和忘记这些类型的管理职责,而不必运行其他检查和什么。
每个事件必须有一个唯一的名称,否则你将杀死已经指定的事件...
示例:
<?php
$ qry =insert into ads values('x','y','z' );;
/ * db execute qry(pdo或what ever)* /
$ adId = //检索广告的唯一引用
$ days = $ _POST ['expire' ] == 16?16:8;
$ qry =CREATE EVENT updateAd _。$ adId。
在CURRENT_TIMESTAMP + INTERVAL时间表。$ days。DAY
ON COMPLETION NOT PRESERVE
DO
update ads set active ='false'where ad_id =。$ adId。;;
// db execute qry(pdo或what ever);
?>
事件存储在mysql数据库中,而不是您自己的,所以您需要一些管理级别的权限。
I made a website where my users can post ads.
Now I want to automatically change one row in their Ads table after a certain time.
Eg: Let say that my user uploads/posts an AD, and before he posts it he gets the choose if he wants to keep it visible for 8 days or 16 days.
Should I check this with a cron or is there a better way to do it?
Visible is called "active" the DB, - tinyint 1 or 0.
thanks in advance,
You have 2 options to manage this both based on the timescale they select.
Store an expiration time with the ad and include expiration > NOW(); in any queries to select ads (this kind of negates the requirement for an 'active' field.
use an event to manage this.
Method 1 may require some admin to 'clean up ads' periodically.
Method 2 - you can't create events in a procedure yet (last time I checked) but they allow you to set and forget these kind of admin duties without having to run other checks and what-nots.
Each event must have a unique name otherwise you will kill the event already specified...
example:
<?php
$qry = "insert into ads values ( 'x', 'y', 'z' );";
/* db execute qry (pdo or what ever) */
$adId = // retriev a unique reference for ad.
$days = $_POST['expire'] == 16 ? 16 : 8;
$qry = "CREATE EVENT updateAd_".$adId."
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL ".$days." DAY
ON COMPLETION NOT PRESERVE
DO
update ads set active = 'false' where ad_id = ".$adId.";";
// db execute qry (pdo or what ever);
?>
The event is stored in the mysql database and not your own so you need some admin level permissions to implement.
这篇关于在一定时间后自动更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!