I need to fetch information from a database table every 10 minutes using PHP language. Does anyone have any idea how I can do this?
I need to fetch information from a database table every 10 minutes using PHP language. Does anyone have any idea how I can do this?
The obvious way is to create an entry in the crontab or in task scheduler to run the desired PHP every 10 minutes.
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
Just schedule a task with these features in scheduler . If you want, you can redirect to a log and facilitate debugging and debug .
c:/caminhoparaoseu/php.exe c:/caminho/para/arquivo.php
Independent of OS, Make sure you use the correct paths for things on your system.
For small intervals of time, you can run a PHP script with an infinite loop at system startup, but calling out of 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 through the browser! Do the command line not to unnecessarily occupy a web server process. In addition, the max_execution_time
directive defaults to 0
from the command line, allowing loop to run indefinitely.
Ideally, you should use cronjob for greater efficiency and keep your code clean. For php you could leave your script in infinite loop:
<?php
set_time_limit(0);
error_reporting(E_ALL);
while (TRUE):
if (functionQueVerificaOtime()):
functionQueExecutaQueryNoBanco();
endif;
endwhile;
This would be logical, but you would have a much bigger problem in leaving that code clean. If you can not find the solution, it will still work, but I recommend using cron from the beginning.
Good luck.
The ideal and highly recommended is creating 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. So the best way is to create the same CronJob. Below is the syntax used to create cronjobs.
[Minute] [Hour] [Day] [Month] [Day of week (0 =sunday to 6 =saturday)] [Command]
The * (asterisk) is used to represent all field values. Example: every minute, or every hour ...
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 hourly at the 30th minute.