Domuta Marcel
Domuta Marcel

Reputation: 509

Schedule update SQL Query using Php

I have an website with mobile phone prices and all prices are collected from external siites manually, all I want is to schedule update prices ($emag, $koyos)every 3 days.

My manuall update query is this:

$editare = "UPDATE modele SET koyos='$koyos', emag='$emag' WHERE id ='{$id}' ";

if ($dbh->query($editare))
 {
   print "<div><h2><img src=\"http://mysite.com/images/ok.png\"/><br /><br /Succes!</h2><br />
  Vei fi redirectionat in cateva momente inspre adaugarea unui nou model de tableta sau telefon!
<meta http-equiv=\"refresh\" content=\"2; URL=index.php\"/>";
}
 else mysql_error(); 

How can I do this query automatic at 3 days?

! My webhost not support cronjob SSH, cPanel or Plesk !

Upvotes: 0

Views: 2180

Answers (1)

Jimbo
Jimbo

Reputation: 26624

Firstly, you need to write a script in PHP that does what you want to do (basically, it's just your PHP code that you want to run at the scheduled time, no additions needed).

Then, using a cron job, you schedule this to run as often as you want.

*/4 * * * * wget --spider file.php in crontab runs every 4 minutes. You might want to read up on some crontab tutorials.

Here's an image from one of the links below describing how to schedule for the time you want:

enter image description here


Cronjobs are basically schedules that run every so often. They're really useful, and I use them all the time on Linux.

The link above is really useful for explaining what numbers you need to type to execute a script every x number of minutes, but you may need to google "Cronjob calculator" to automatically calculate the right numbers for you (I do this myself sometimes).

In linux, with crontab installed, type crontab -l to see a list of all cronjobs. Type crontab -e to edit your cronjob list for the current user. This is where you paste the code I gave you above.

Edit: To get your head around cronjobs at a basic level, first install crontab. Then, use the code above to execute a script that writes to a file. Then, just tail -f that file, and watch how (in four minutes after the cronjob starts) it updates in front of your eyes.

Start with the basics. Then get it to execute your MySQL script.

Useful Links:

http://www.linuxweblog.com/crotab-tutorial
http://clickmojo.com/code/cron-tutorial.html (this looks good)
http://www.tutorial5.com/content/view/95/51/
http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/ (minutes / days explanation)

Note: You can't do less than a minute. The last link suggests a sleep() command to get around this, so check that out.


The problem with OP's question is that because he uses very restricted shared hosting, he doesn't have access to SSH, cPanel, cronjobs or anything else like that.

An option here is to spin off a separate process for wgetting the PHP file using exec(), or shell_exec(), with nohup and an ampersand (&) at the end, along with a long sleep() time for the number of days the OP wants. This will cause the PHP script to run in the background on the server.

Safeguards need to be put in place with the above example, ie: if the server is rebooted then the script will no longer be running and will have to be restarted. Conversely, if the user accidentally hits the script at the wrong time (or someone else does) then multiple processes will be spawned, so a safeguard to check whether the process is already running for example could occur (or a basic password entry requirement may help to avoid accidental hits).

Upvotes: 4

Related Questions