每年使用cron job更新数据库

时间:2013-04-09 16:15:20

标签: php mysql

我想在php项目中使用 cron job 更新mysql数据库。下面是我想要每年运行的代码段。我已经尝试在mysql中使用事件调度程序,但我失败了。

$stud="select * from student_class where class<4";
 while ($row=mysql_fetch_array($stud))
 {
  $sql = "INSERT into student class(id,student_id,class,year) values('','{$row  
 ['student_id']}','{$row['class']}','{$row['year']}+1')" ;
  mysql_query($sql);
 }

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

使用此documentation可帮助您在服务器上添加cron作业

crontab -e
1 2 3 4 5 php /path/to/php_file arg1 arg2

其中:

1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/php_file- Script or command name to schedule

所以你的cron工作看起来像是在每年的1月1日00:00执行:

0 0 1 1 * php /path/to/php_file arg1 arg2

答案 1 :(得分:1)

安装了php-cli,以便您可以从终端运行脚本。在你的脚本中,php二进制文件的位置应该在标题中,例如#/ usr / bin / php 让脚本在终端环境中工作。

然后按照Philippe的建议编辑crontab。