0

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

Kaki Baleven
  • 355
  • 3
  • 7
  • 19
  • @Dagon , not every seconds, every day or week, the most important thing is that I dont want to enter the delete page every time, I want that the deleting script will run automuticly every some time – Kaki Baleven Sep 09 '15 at 22:49
  • 2
    cron job, theses run from the command line in the background so no user iteration issue, don't bother with sleep –  Sep 09 '15 at 22:49
  • @Dagon where can I find documention of cron job? I search in php.net, didnt found. – Kaki Baleven Sep 09 '15 at 22:50
  • they are not a function of php, but the os -*.nix http://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php –  Sep 09 '15 at 22:52
  • If you run this from the command line it will output as it runs, but if you run it from a browser it will wait until it's finished everything before delivering the output to the browser – scrowler Sep 09 '15 at 23:00
  • i don't think he wants any output that was just a test. –  Sep 09 '15 at 23:01

2 Answers2

3

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
  • MIN = "Minute" (0 to 59)
  • HOUR = "Hour" (0 to 23)
  • DOM = "Day of Month" (1-31)
  • MON = "Month" (1-12)
  • DOW = "Day Of Week" (0-6)

Here are some other really practical examples: http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

0

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.

Anshul
  • 103
  • 9