php页面最大执行时间 set_time_limit函数不起作用
导读:
set_time_limit 不生效或者无效解决方法
<?php
global $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."',
<?php
set_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函数不起作用 的内容,更多相关内容请关注PHP中文网
相关推荐
Thinkphp6.0 获取绝对路径总结
Thinkphp6.0 获取绝对路径,改到了App 基础类里面获取,可通过助手函数获取: app()>getRootPath() //获取应用根目录 app()>get…
2022-06-30 16:33:52 | 分类:PHP | 作者:myerob | 阅读:507 | 标签:TP6 路径 | 收藏
php页面最大执行时间 set_time_limit函数不起作用
set_time_limit 不生效或者无效解决方法<?php global $begin; global $end; $begin = microtime(TRUE); try { $dsn …
2022-07-19 08:12:49 | 分类:PHP | 作者:myerob | 阅读:234 | 标签:php 页面 最大执行时间 set_time_limit 函数 | 收藏
keil下的string.h函数说明
(1).strchr原型:extern char *strchr(const char *s,char c); const char *strchr(const char* _Str,int _Val…
2022-07-20 14:55:50 | 分类:C语言 | 作者:myerob | 阅读:237 | 标签:keil string.h | 收藏
PHP json_decode()报错 json_last_error()判断错误类型及解决方法
PHP json_decode()报错 json_last_error()判断错误类型 解决背景:最近在用curl请求接口获取json数据串的时候,一直出现PHP中json_decode()解析字符串…
2022-10-29 21:45:57 | 分类:PHP | 作者:myerob | 阅读:620 | 标签:PHP json_decode() 报错 json_last_error() 判断错误类型 解决方法 | 收藏
- 阅读榜
- 1. U.FL座 IPEX/IPX接头1代,2代,3代,4代,5代区分对照图
- 2. 什么情况下免征教育附加税,地方教育附加税?两附加费优惠政策
- 3. ipex1代与4代差别
- 4. 个人所得税申报系统(扣缴端)如何删除已离职人员信息
- 5. NTC热敏电阻B值3950(温度系数对照表)
- 6. mounRiver/eclipse报错“Symbol could not be resolved”,但是编译没有问题
- 7. 概率统计学中95%置信区间的系数1.96怎么得到的?
- 8. 单片机测量NTC热敏电阻温度,函数计算方式。
- 9. Linux netstat 命令详解 ssh
- 10. JS实现可控制左右方向的无缝滚动效果代码分享