php頁面最大執行時間 set_time_limit函數不起作用

來源:互聯網
上載者:User
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 — 設定指令碼最大執行時間

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.