简体   繁体   English

如何每5分钟重复一次mysql查询?

[英]How can I repeat a mysql query every 5 minutes?

I have an sql query that looks like this: 我有一个看起来像这样的sql查询:

<?php
    include("settings.php");

    $sql = conn->query("SELECT * FROM table LIMIT 1");

    $content = $sql->fetch_assoc();

    $id = $content['id'];

    /* PHP Operations here. */
    ?>

Now, I want to be able to repeat this sql query every lets say 5 minutes, because the tables content will change. 现在,我希望能够每隔5分钟重复一次此sql查询,因为表内容将更改。 How can I do this? 我怎样才能做到这一点? Is there a way to do an sql request in a javascript or ajax script? 有没有办法在javascript或ajax脚本中执行sql请求?

如果只想刷新内容,则在settimeout()中使用AJAX调用。

Solution

Use a cron job. 使用cron作业。 If the file you are showing is "/var/www/some/file.php" then just execute the following as the user which runs the web server (for example, "www-data" or "apache"): 如果您显示的文件是“ /var/www/some/file.php”,则只需以运行Web服务器的用户身份执行以下命令即可(例如,“ www-data”或“ apache”):

{
    # Will output to stdout the current user's crontab
  crontab -l;
    # Add a line to execute the php script every 5 minutes
  echo '*/5 * * * * '"$(which php5) /var/www/some/file.php";
} | crontab - # The current user's crontab will be replaced by stdin

Assumptions 假设条件

  • You're using Linux. 您正在使用Linux。
  • You're using apache httpd. 您正在使用apache httpd。

Background 背景

PHP web applications are PHP scripts which are run by the web server in response to an HTTP request. PHP Web应用程序是由Web服务器响应HTTP请求而运行的PHP脚本。 What you need in here is to run the PHP script not in response to an HTTP request but in response to a time event, say, 5 minutes have passed since the last run. 这里您需要的是运行PHP脚本,而不是响应HTTP请求,而是响应时间事件,例如,自上次运行以来已经过去了5分钟。

Well, according to the Q&A the real problem would have a solution like this: 好吧,根据问答, 真正的问题将有一个这样的解决方案:

Files 档案

  1. You have one PHP file which is directly requested by the user, which I will call "/index.php", located at "/var/www/index.php". 您有一个直接由用户请求的PHP文件,我将其称为“ /index.php”,位于“ /var/www/index.php”。 This has the presentation of the page and some javascript. 这里有页面的介绍和一些javascript。
  2. You have a second PHP file which is requested by the user indirectly, which I will call "/bg.php", located at "/var/www/bg.php". 您有第二个由用户间接请求的PHP文件,我将其称为“ /bg.php”,位于“ /var/www/bg.php”中。 This file's content only has the routines to update the database and returns javascript code or something else. 该文件的内容仅具有更新数据库并返回javascript代码或其他内容的例程。

index.php index.php

Here you will have something like this: 在这里,您将看到以下内容:

<script type="text/javascript">
/* Have jquery included before */

function poll_the_server(){
    /* Get the data from the server with ajax */
  var jqxhr = $.ajax( "/bg.php" )
    .always(function(){
         /* Prepare the next run */
        setTimeout(poll_the_server, 300);
      })
    .done(function(){
        /* Succeded: Do whatever you want with your data */
      })
    .fail(function(){
        /* Error: you may ask the user what to do */
      });
}

  /* Do the first call */
setTimeout(poll_the_server, 300);
</script>

Edit: 编辑:

References 参考文献

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

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

相关问题 如何每 5 分钟重绘一次画布仪表板? - How can I redraw a canvas dashboard every 5 minutes? 我想每 30 分钟倒计时一次,怎么做? - I want to run the countdown every 30 minutes, how can that be done? 每5分钟如何重新加载或重启一次功能 - How can i reload or restart function after every 5 minutes 如何每“t”分钟刷新一次 JWT 令牌? - How can I refresh JWT token every "t" minutes? 如何为dom重复中的firebase-query提供唯一的ID,以便每次获取数据时都可以清除其路径? - how to give a unique id to firebase-query inside a dom repeat so i can clear its path every time data is fetched? 是否可以在Javascript中每10分钟重复一次循环? - Is it possible to repeat a loop every 10 minutes in Javascript? 我有由setInterval每3分钟调用一次的JS函数-当页面也加载时如何调用它? - I have the JS function that is invoked every 3 minutes by setInterval - how can I invoke it when page loads too? 如何在满足条件之前重复 Firestore 查询? - How can I repeat a Firestore query until a condition is met? JavaScript:如何在Selenium webdriverjs中每两分钟安排一次任务? - JavaScript: How can I schedule a task every two minutes in selenium webdriverjs? 如何每x分钟将GPS位置发送到mysql表 - How to send GPS location to mysql table every x minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM