简体   繁体   English

运行独立的MySql查询

[英]Running independent MySql queries

I was just wondering is there any way to store a bit of php script separate from the actual webpage that runs constantly on the server. 我只是想知道是否有任何方法可以存储与在服务器上不断运行的实际网页分离的php脚本。

For example I have this MySql query; 例如,我有这个MySql查询; $sql = "UPDATE users SET connection= IF(NOW() - last_access_time > 3, 0, 1)"; $ sql =“更新用户设置连接= IF(NOW()-last_access_time> 3,0,1)”;

basically it checks every user on the database which are currently using the website. 基本上,它会检查数据库中当前正在使用该网站的每个用户。 The requirement of the website force me to do this every 2 seconds on almost every page. 网站的要求迫使我几乎在每个页面上每2秒执行一次此操作。

it seems like it creates a lot of traffic on the server side. 似乎在服务器端会产生大量流量。 So is there any way to run this query separate from the website? 那么,有什么方法可以与网站分开运行此查询吗? Or is there any other way to solve this problem in general? 还是有其他一般方法可以解决此问题?

Not sure how to abstract out this particular case, but it looks to me like you're trying to maintain a record in your database of whether the user is currently active on the site. 不知道如何提取这种特殊情况,但是在我看来,您正在尝试在数据库中维护有关用户当前是否在站点上处于活动状态的记录。

How about this instead? 怎么样呢?

On every page run this: 在每个页面上运行以下命令:

$sql = "UPDATE users SET last_access_time = NOW() WHERE id=".$_SESSION['USERID'];

Then in your scripts where you are looking for users with a connection do it this way: 然后在您要查找具有连接用户的脚本中,通过以下方式进行操作:

$sql = "SELECT * FROM users WHERE last_access_time < NOW()-3";

The way you're running it now, your db server has to run through every user everytime someone hits a page, to set the status to active or not. 您现在运行它的方式,您的数据库服务器必须在每次用户访问页面时都要遍历每个用户,以将状态设置为活动或不活动。 Instead you could only hit the db everytime you actually need that information. 相反,您只能在每次实际需要该信息时才访问数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM