开发者

Get a mysql_query script to execute 30 seconds after visiting the php site [duplicate]

开发者 https://www.devze.com 2023-03-31 02:02 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Php mysql to do task after 30 seconds
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Php mysql to do task after 30 seconds

is there a way to get a mysql_query script to execute 30 seconds after visiting t开发者_运维知识库he php site.

This would be on a mobile device specifically the android device. After my user clicks submit i would like the mysql_query script to run after 30 seconds has passed.


Directly no, because PHP is Request based, which means after the request nothing is running anymore.

You could however create some kind of queue (e.g. in the database) and let a background worker work the queue. That can eighter be a PHP script called by a cron job every minute (but then it may get executed 60s later, but easy to implement) or a deamon on the server running in the background (hard(er) to implement and you need access to the server).


You could use javascript, for example with jQuery this might look something like:

$('#form').submit(function(e) {
    e.preventDefault(); // stop form from submitting when submit is clicked
        setTimeout(function() { // set a 30 second timer to make an ajax request to your php script
        $.ajax('mysqlscript.php');
    }, 30000);
});


You don't need that.
There are not a single reason to execute SQL queries with delay.
Just run it immediately.


Javascript options proposed here are not safe because if user closes the browser, the callback will never be performed.

Using a cron job isn't an option if you really need to execute the script 30 seconds later, but it's ok if you need to execute "some time later". To make a daemon is the best option, but it's difficult to implement and it isn't the most "compatible" option.

Try this (I haven't tested!!): Instead to make a simple AJAX call or a complete page access, make another AJAX call when page is completed:

// This is the page you load and 30 seconds after it is loaded you want to call mysql
....
$(document).ready(function() {
   $.ajax({
     url: "callMYSQL.php",
     async: true
   });
});

When document is loaded (case you load a new page, it you call it through AJAX you just have to call AJAX twice), it will call to callMYSQL.php. This PHP file must wait 30 seconds (sleep(30)) and later do whatever you need.

Note: This solution needs you increase the time a PHP script can be alive. Note2: I can't understand why you need it, perhaps you should redesing your app.


I think you need to use Javascript for this.

eg: using the setTimeout() function.

$query4=mysql_query("select id, price from pricelist where id='$result3[id]'");
$result4=mysql_query($query4);

// **Example:**
function queryTimeout(){
  var t=setTimeout("doQuery()",30000);<br />
}

function doQuery(){
  $query4=mysql_query("select * from table");
  $result4=mysql_query($query4);
}

However I am no javascript expert, this is just off the top of my head.


How about rethinking the problem- you want to be able to find all users who clicked the button over 30 seconds ago right?

You don't need to wait 30 seconds to perform the query: when they click the button, you execute the SQL query immediately, and update a field on the user's table called something like time_clicked.

Then you can just find the users you want by performing a query:

<?php
    mysql_query("SELECT * FROM users where time_clicked < '" .
        date('Y-m-d h:i:s',strtotime('-30 seconds')) . "'");
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消