doupin5667 2013-03-21 09:36
浏览 32
已采纳

使用Php安排更新SQL查询

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 !

  • 写回答

1条回答 默认 最新

  • douba1214 2013-03-21 09:41
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致