set_time_limit 不生效或者無效解決方案
<?phpglobal $begin;global $end;$begin = microtime(TRUE);try {$dsn = "mysql:host=localhost;dbname=kaixin";$db = new PDO($dsn, 'root', '111');$db->exec("set names 'utf8'");//預設編碼$db->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);for($i=1; $i<5000; $i++){$sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value ('文章標題".$i."', '內容".$i."','1227237892','kaixin','127.0.0.1')";$db->exec($sql);}$end = microtime(TRUE);// by bbs.it-home.org$time = $end-$begin;echo "執行了".$time."s";} catch (Exception $e) {echo "Failed: " . $e->getMessage();}?>
執行這段代碼,只能插入大概1000條左右的資料,這樣頁面就不執行了。 測試頁面的執行時間大概都是29秒左右,也就是頁面的最大執行時間 是30秒。
知道set_time_limit函數,可以設定頁面執行時間。
set_time_limit函數用法如下:
本函式用來設定該頁最久執行時間。內定值是 30 秒,在 php.ini 中的 max_execution_time 變數設定,若設定為 0 則不限定最久時間。當執行到該函數時,才開
始計算。例如,若內定是 30 秒,而在執行到該函數前已執行了 25 秒,而用本函式改為 20 秒,則該頁面最長執行時間為 45 秒。
把這個set_time_limit函數加到了程式裡,
發現頁面執行時間還是29秒左右,沒有起作用。
看到網上有人說,php.ini裡的safe_mode如果為on,本函數不執行。查了我的php.in裡的safe_mode=off。
試著把set_time_limit(0);加入到for迴圈裡。
<?php...for($i=1; $i<5000; $i++){set_time_limit(0);$sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value ('文章標題".$i."', '內容".$i."',<?phpset_time_limit(0);global $begin;global $end;$begin = microtime(TRUE);...?>'1227237892','kaixin','127.0.0.1')";$db->exec($sql);}...?>
也沒有起作用。最後在網上查到,有人說,“set_time_limit函數最好是在linux下執行,windows執行可能也無效”。徹底對這個函數失去信心 了,估計是因為我是windows系統的原因。
只能修改php.ini裡的max_execution_time = 30了。這個預設是30秒,我修改為max_execution_time = 300.重新啟動apache伺服器。 結果執行時間為140秒左右,5000條資料終於插入了。 看來,windows下這個頁面執行時間還是在php.ini裡控制,修改max_execution_time。 如果用PDO往MYSQL裡面插入資料的話。插入五千條大概140多秒時間,插入1萬條大概260多秒時間。
以上就是php頁面最大執行時間 set_time_limit函數不起作用 的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!
相關文章:
php set_time_limit()設定頁面執行時間
php set_time_limit(0) 設定程式執行時間的函數
set_time_limit — 設定指令碼最大執行時間