如何在特定时间使用MySQL或PHP运行查询? [英] How can I run a query in a specific time using MySQL or PHP?

查看:106
本文介绍了如何在特定时间使用MySQL或PHP运行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在特定时间(例如每秒钟/分钟)运行查询.顺便说一下,我正在构建一个简单的拍卖系统,而我正在谈论的查询是检查是否有已经过期的产品.表示其datetime_end小于当前日期和时间.所以我有这样的查询:

is it possible to run a query in a specific time like for example every seconds/minutes. By the way I am building a simple auction system and the query I am talking about is to check if there are products that are already expired. Means their datetime_end is less than the current date and time. So I have a query like this:

SELECT id, datetime_end FROM auction_product WHERE datetime_end < NOW() AND `status` = 0;

在确定是否有过期产品之后,我将创建一个查询,将其状态更改为 1 ,表示状态为 cancelled/closed .

And after identifying if there are products that are already expired I will create a query that will change their status to 1 means the status is cancelled/closed.

MySQL中是否有可以运行特定查询的函数?喜欢自动查询.还是我应该制作一个每秒钟运行一次的Ajax,并检查是否有过期的产品?你能给我一些想法吗?因为在我当前的设置中,我只是将AJAX放在了产品页面和主页中.意味着如果用户在另一个页面中,我的AJAX将不会运行.所以我想我将创建的AJAX放在页眉或页脚或全局JS中.如果这样做,是否会影响页面加载的性能?因为它将每秒钟运行一次.

Is there a function in MySQL that can run a certain query? Like automatic query. Or should I make an Ajax that will run every seconds and check if there is an expired product? Can you give me some idea about this? Because in my current setup I just put the AJAX in product page and homepage. Means if the user is in the other page my AJAX will not run. So I think I will put my created AJAX somewhere in the header or footer or in a global JS. If I did that does it affect the performance of my page load? Because it will run every seconds.

非常感谢.我希望你能给我一些想法.:)

That's all thanks. I hope you can give me some idea about this. :)

推荐答案

您无需每秒在客户端进行检查.万一有人访问产品页面,您将在其中运行ajax来检查是否有足够的产品.如果没有剩余产品,则可以在页面本身上更新数据库.

You don't need to check it every second on the client side. In case someone visits the page of the product, you'll run an ajax there to check if there are enough products left or not. If there are no products left you can update the database on the page itself.

现在,您还想确保它得到定期更新,因此您可以在服务器端的 Cron Job 上运行脚本.但是,您还需要确保不要在上面运行一些占用大量资源的脚本.您可以大约每小时或每两个小时运行一次cron作业,以从服务器端定期对其进行更新.并且,如果有任何用户查看产品,则将使用ajax自动更新产品,因此,下次用户访问两个cron作业之间时,由于先前的用户,他们将看到页面已被更新.这样可以通过分配工作来减轻服务器的压力.

Now you also want to make sure it is regularly updated, so you can run a script on the server side on a Cron Job. But, you also need to make sure you don't run some heavy resource intensive scripts on it. You can run a cron job about every hour or two hours to regularly update it from the server side. And in case any of the users views a product, you will update it automatically with the ajax, so the next time a user visits, in between two cron jobs, they will see the page being already updated because of the earlier user. This will keep the pressure out of your server by distributing the work.

所以这个想法有点像这样:

So the idea is somewhat like this:

 1)user enters-> visits page-> runs ajax to check if products are left -> update db if products are over
2)cron job checks if products are left every two hours-> updates db if products are over 

这篇关于如何在特定时间使用MySQL或PHP运行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆