Search database table information with PHP every 10 minutes

Asked

Viewed 458 times

2

I need to search for information from a database table every 10 minutes using PHP. Does anyone have any idea how I can do this?

  • 1

    The ideal would be a cronjob.

  • I don’t have the code yet. I need an idea of what I can use in PHP to do this. Example... I should use, Curl, cronjob, Windows task scheduler etc. cronjob is a good... I know it works great on Linux, I’m seeing how it works on Windows.

  • Why not use XMLHttpRequest + window.setTimeout? Is it something specific to "real time"? What is the ultimate goal of this?

  • See if this helps: http://answall.com/a/32901/70

3 answers

3

The obvious means is to create an entry in the crontab or in the task scheduler run the desired PHP every 10 minutes.

Linux

In most distros *Nix the crontab line would look like this, to run every 10 minutes:

5,15,25,35,45,55 * * * * /usr/bin/php meuscript.php > /var/log/meuscript.log

Some distros support this syntax:

*/10 * * * * /usr/bin/php meuscript.php > /var/log/meuscript.log


Windows

Just schedule a task on scheduler with these features. If you want, you can redirect to a log and facilitate the detection of errors and debug.

c:/caminhoparaoseu/php.exe c:/caminho/para/arquivo.php


Independent of OS, make sure to use the right paths for things in your system.


Plan B: looping with PHP

For short intervals of time, you can run a PHP script with a loop infinite at system startup, but calling outside the browser:

<?php
   set_time_limit( 0 ); // para o PHP poder rodar sem limite de tempo

   while( true ) {
       ... aqui vai a sua função do DB ...

       sleep( 60 ); // numero de segundos entre um loop e outro
   }
?>

But do not access this last script from the browser! Use the command line not to unnecessarily occupy a web server process. Also, the directive max_execution_time has default value 0 by the command line, allowing the loop rotate indefinitely.

1

The ideal and highly recommended is to create a cronjob on the server by calling the PHP file that does the job. There is nothing for PHP like the Quartz library for Java. Therefore, the best way is to create Cronjob yourself. Below is the syntax used to create cronjobs.

[Minute] [Hour] [Day] [Month] [Day of week (0 =sunday to 6 =saturday)] [Command]

* (asterisk) is used to represent all field values. Example: every minute, or every time...

Example of a cron call:

30 * * * * /usr/bin/php -f <caminho do seu script> &> /dev/null

In the above command, the call will be executed every hour in the 30th minute.

Source: How to create cron job using PHP?

1

the ideal would be for you to use cronjob for greater effectiveness and keep your code clean. By php vc could leave your script in infinite loop:

<?php

set_time_limit(0);
error_reporting(E_ALL);

while (TRUE):
    if (functionQueVerificaOtime()):
        functionQueExecutaQueryNoBanco();
    endif;
endwhile;

That would be logical, but you’d have a much bigger problem clearing that code. If there is no way the solution will still be this but I recommend from the beginning to use cron.

Good luck.

  • It works, but the chances of overloading the process are high. Perhaps a front-end solution such as XMLHttpRequest + window.setTimeout, even though it’s not what he asks for, maybe he doesn’t have much knowledge of when to use the front-end and back-end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.