I want to run SQL query every day.
Now I read about the sleep
function.
I understand that the script runs until it get to the sleep function, and than sleep some seconds and then wake up again, so I did for testing this code:
for ($i = 1; $i < 3; $i++) {
echo date("i:s");
sleep(10);
}
The problem is that I dont get any output until go through 20 seconds.
And I dont need that, I need that the script run, than sleep 10 seconds and than run again?
From what I understand its only happens with output, I mean the behavior of the script above is to echo in the first iteration but save the output and then when the two times over output all.
So if I'm right, If I run a SQL query it will run the first iteration and than sleep 10 sec and than run again yes?
Also I have a question, whats happen if I exit the page, the script still keep running? or when I leave the page its kill the script?
I want to run something like that:
function delPosts () {
sleep(24 * 60 * 60);
$mysqli->query('DELETE FROM posts WHERE date < "SOME DATE"');
delPosts();
}
delPosts();
Tanks for helping
What You Should do:
You don't need to mess with sleep etc etc.
Instead just put the full query action in a script that doesn't need any user interaction. Hardcode the information and store it NOT in a web directory with as locked-down permissions as possible.
To run a mysql query (such as in a PHP script) every day you should probably use a cron job.
How To Set Up Cron Jobs:
On a linux machine you would set this up by modifying /etc/crontab. Basically crontab controls what and when to run on an automated cycle. (These are cron jobs)
To run a script (/home/user/bin/runQuery.php) every 5 minutes add this line to crontab:
*/5 * * * * php /home/user/bin/runQuery.php
(full path to php may be required)
Here's what's happening:
MIN HOUR DOM MON DOW /path/to/script.php
Here are some other really practical examples: http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
This is very easy. you just need to use a do-while loop instead of for loop. like this
$i=0;
do{
echo date("i:s");
sleep(10);
}while($i>=3);
That's it your problem will be solved.