Set_time_limit not effective or invalid workaround
<?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 '");//default encoding $db->setattribute (Pdo_attr_errmode, Pdo_errmode_ EXCEPTION); for ($i =1; $i <5000; $i + +) {$sql = "insert INTO ' article ' (' title ', ' content ', ' time ', ' Author ', ' IP ') value (' Article title ". $i." ', ' content '. $i. "', ' 1227237892 ', ' kaixin ', ' 127.0.0.1 ')"; $db->exec ($sql);} $end = Microtime (TRUE);/by Bbs.it-home.org$time = $end-$begin; echo "performed". $time. " S ";} catch (Exception $e) {echo "Failed:". $e->getmessage ();}? >
Executing this code can only insert about 1000 or so of data, so the page will not be executed. The test page execution time is about 29 seconds, that is, the maximum execution time of the page is 30 seconds.
Knowing the Set_time_limit function, you can set the page execution time.
The Set_time_limit function uses the following:
This function is used to set the maximum execution time for the page. The default value is 30 seconds, the Max_execution_time variable is set in PHP.ini, and if set to 0, it is not limited to the longest time. When the function is executed, the
The initial calculation. For example, if the default is 30 seconds and has been executed for 25 seconds before executing to the function, and if you have changed to 20 seconds with the functor, the maximum execution time for this page is 45 seconds.
Add this set_time_limit function to the program,
found that the page execution time or about 29 seconds, no effect.
See online Some people say, php.ini in the Safe_mode if is on, this function does not execute. Check out the Safe_mode=off in my php.in.
Try to put Set_time_limit (0) into the For loop.
<?php...for ($i =1; $i <5000; $i + +) {set_time_limit (0); $sql = "INSERT INTO ' article ' (' title ', ' content ', ' time ', ' Author ', ' IP ') value (' article title '. $i. "', ' contents '. $i." ', <?phpset_time_limit (0); global $begin; global $end; $begin = Microtime ( TRUE);..? > ' 1227237892 ', ' kaixin ', ' 127.0.0.1 ') "; $db->exec ($sql);} ...? >
It didn't work. Finally, on the internet, it was said, "The Set_time_limit function is best done under Linux, Windows execution may also be invalid." Completely lost confidence in this function, it is estimated because I am the cause of Windows system.
Can only modify the php.ini in the Max_execution_time = 30. This default is 30 seconds, I changed to Max_execution_time = 300. Restart the Apache server. The result execution time is about 140 seconds, and 5,000 data is finally inserted. It seems that the page execution time under Windows is controlled in php.ini, modified max_execution_time. If you use PDO to insert data into MySQL. Insert 5,000 for about more than 140 seconds and insert 10,000 for about more than 260 seconds.
The above is the PHP page maximum execution time set_time_limit function does not work content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!
Related articles:
PHP set_time_limit () set page execution time
PHP set_time_limit (0) Set the program execution time function
set_time_limit-setting the maximum execution time of a script