查询设定的时间? [英] Querying for a set time?

查看:37
本文介绍了查询设定的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 PHP 脚本中查询设定的时间?

Is there a way to query for a set time from a PHP script?

我想编写一个 PHP 脚本,该脚本获取一个 id,然后查询 MySQL 数据库以查看是否存在匹配项.如果其他用户可能尚未上传他们的匹配项,那么我的目标是查询直到找到匹配项或直到 5 秒过去,然后我将返回 0.

I want to write a PHP script that takes an id and then queries the MySQL database to see if there is a match. In the case where another user may have not yet uploaded their match, so I am aiming to query until I find a match or until 5 seconds have passed, which I will then return 0.

在伪代码中,这就是我的想法,但它似乎不是一个好方法,因为我读过循环查询不是好的做法.

In pseudocode this is what I was thinking but it doesn't seem like a good method since I've read looping queries isn't good practice.

$id_in = 123;
time_c = time();
time_stop = time + 5; //seconds

while(time_c < time_stop){
     time_c = time()
     $result = mysql_query('SELECT * WHERE id=$id_in');
}

推荐答案

听起来您的要求是轮询某个表,直到出现具有特定 ID 的行.你需要一个这样的查询来做到这一点:

It sounds like your requirement is to poll some table until a row with a particular ID shows up. You'll need a query like this to do that:

SELECT some-column, another column FROM some-table WHERE id=$id_in

(专业提示:不要在软件中使用 SELECT *.)

(Pro tip: don't use SELECT * in software.)

看来你想轮询五秒钟然后放弃.那么让我们来解决这个问题.

It seems that you want to poll for five seconds and then give up. So let's work through this.

一种选择是简单地sleep(5),然后使用您的查询轮询表.这样做的好处是非常简单.

One choice is to simply sleep(5), then poll the table using your query. The advantage of this is that it's very simple.

另一种选择是你所拥有的.这将使您的 php 程序尽可能快地敲打桌子,一遍又一遍,直到投票成功或直到您的五秒钟用完为止.这种方法的优点是当其他程序命中表时,您的 php 程序不会处于休眠状态.换句话说,它将以最小的延迟获取对表的更改.然而,这种选择有一个巨大的缺点.通过尽可能快地敲打桌子,您将占用 MySQL 服务器上的资源.这通常是浪费的.它会阻止你的应用程序有效地扩展(如果你有一万个用户都这样做呢?)具体来说,它可能会减慢其他程序试图访问表的速度,因此它无法在五秒钟内完成更新.

Another choice is what you have. This will make your php program hammer away at the table as fast as it can, over and over, until the poll succeeds or until your five seconds run out. The advantage of this approach is that your php program won't be asleep when the other program hits the table. In other words, it will pick up the change to the table with minimum latency. This choice, however, has an enormous disadvantage. By hammering away at the table as fast as you can, you'll tie up resources on the MySQL server. This is generally wasteful. It will prevent your application from scaling up efficiently (what if you have ten thousand users all doing this?) Specifically, it may slow down the other program trying to hit the table, so it can't get the update done in five seconds.

但是有中间立场.尝试等待半秒

There's middle ground, however. Try doing a half-second wait

 usleep(500000);

在每次轮询表格之前.那不会那么严重地浪费 MySQL 资源.即使你的 php 程序在其他程序撞到桌子时处于休眠状态,它也不会休眠很长时间.

right before each time you poll the table. That won't waste MySQL resources as badly. Even if your php program is asleep when the other program hits the table, it won't be asleep for long.

这篇关于查询设定的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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