2013-04-09 90 views
0

我想在php項目中使用cron job更新mysql數據庫。下面是我想要每年運行的一段代碼。我曾嘗試在MySQL中使用事件調度程序,但我失敗了。每年使用cron作業更新數據庫

$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

使用此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作業看起來像這樣以00:00的1月1日執行每年:

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

好的,我試試dat – internally1

1

安裝了php-cli,以便您可以從喲運行腳本你的終端。在你的腳本中,php二進制文件的位置應該在頭文件中,例如#/ usr/bin/php 讓腳本在終端環境中工作。

然後按照Philippe的建議編輯crontab。