I have a request that should, after the expiration of the minute, only perform an update, and it immediately executes ignoring time

$this->db->query(" update colban set `go` = `go` -1 where id = '".(int)$this->url[450]."' < ".( time()-60*1 )); } 
  • can events help - kursof
  • five
    Are you absolutely sure that this is exactly what you want? Can you describe why you need this behavior and look for more correct alternatives? - rjhdby

3 answers 3

As many have said, you do not need it.

The reason for this question is the lack of understanding of the elementary bases of databases.

The value should not be checked by the script that writes the data, but by the one that reads.

That is, in this case, we do not write a specific value to the database at the right time, but write the desired time for changing the value.

And all reading scripts will read this time and compare with the current one. If more, then the value has changed. Very simple, and does not require any crutches from other answers. This is how databases all over the world work.

  • reads select, that is, it must first read, and if the value is equal to the desired value or 0, then it makes a request for update // and how to implement the record so that everything is correct - kursof
  1. The WHERE clause in the SQL query only specifies a condition that will satisfy the record if it is met. It does not control the execution time of the request in any way.
  2. Where did you get this syntax from?
  3. Writing a query like this is an extremely bad practice. Learn how mysqli_stms_bind_param works .
  4. In order to postpone the execution of any team there are a lot of options, but all of them are crutches to some extent.

For example:

  • You can create a JOB in mysql, which will periodically read the queue table (in which you will enter update tasks) and perform those that are older than a minute
  • You can stupidly slow down the execution of the script operator sleep(60)
  • It is possible by curl (with session break) to call a script that will perform the update in a minute
  • You can save tasks for update to a file (or database table) and start processing by cron (almost the same as the first option)
  • You can ponder with events
  • You can look at the FPM and its fastcgi_finish_request function
  • Etc. etc.
  • @Alex yes, I missed a bit. Of course mysqli_stms_bind_param - rjhdby
  • Well, the task is that after a request by reference, a certain time will be counted and then the request will be executed in the database with a minute report for example - kursof
  • well, why? The question does not indicate the version of php, so you should just remind the participant about it. - Alex
  • @kursof You want the condition to be met through the defined. Is time respected in the mysql query itself? why not use php tools ( sleep() )? - Alex
  1. Option 1 - use the mysql SLEEP () function, DO SLEEP(60);

    An example mysql query:
    DO SLEEP(60);
    UPDATE ...
    you can use SELECT SLEEP(60) , but in this case you need to consider that the output will always be 0 or 1.

  2. Option 2 - use the php sleep () function.

    Example:
    SLEEP(60);
    $this->db->query("...");

Both options delay execution for N seconds.


These functions should not be used if you execute the next request only after receiving the results of the previous one, because, for example, a consecutive call of 2 requests will result in an idle time of 2 * N seconds.

  • What about performance? will it eat or not if it is not a single request, but a lot from the different ones - kursof
  • @kursof did not test on many and from different ones, but in fact the functions just postpone execution and their performance should not affect their performance. - Alex
  • your option is good, but it loads the page exactly 60 seconds, and then it performs, it will not work like that ( - kursof
  • It is very sad that on this site, plus harmful answers to meaningless questions. - Ipatiev
  • 2
    @ Ipatiev :) I was asked to show the option with sleep and I showed) As for the conclusions, after clarification from the author about a variety of requests, I can not say that this is the appropriate answer (added in the answer), but I can not agree on its harm, because There is no condition about the set of requests in the question and the request is actually executed after sleep . Thus, it meets the condition of the task должен по истечению минуты только выполнять update . - Alex