php超过30秒,最大执行时间超过30秒(maximum execution time of 30 seconds exceeded php)

最大执行时间超过30秒(maximum execution time of 30 seconds exceeded php)

当我运行我的脚本时,我在处理所有数据行之前收到以下错误。

最大执行时间超过30秒

在研究问题之后,我应该能够延长应该解决问题的max_execution_time时间。

但是,在我的PHP编程初期,我想知道下面是否有更优化的方式来执行我的脚本,所以我不必依赖“退出监狱卡”。

该脚本是:

1拍摄CSV文件

2樱桃采摘一些列

3尝试将10k行CSV数据插入到我的SQL表中

在我的脑海中,我想我应该能够插入块,但这远远超出了我的技能,我甚至不知道如何写一行:

提前谢谢了

function processCSV()

{

global $uploadFile;

include 'dbConnection.inc.php';

dbConnection("xx","xx","xx");

$rowCounter = 0;

$loadLocationCsvUrl = fopen($uploadFile,"r");

if ($loadLocationCsvUrl <> false)

{

while ($locationFile = fgetcsv($loadLocationCsvUrl, ','))

{

$officeId = $locationFile[2];

$country = $locationFile[9];

$country = trim($country);

$country = htmlspecialchars($country);

$open = $locationFile[4];

$open = trim($open);

$open = htmlspecialchars($open);

$insString = "insert into countrytable set officeId='$officeId', countryname='$country', status='$open'";

switch($country)

{

case $country <> 'Country':

if (!mysql_query($insString))

{

echo "

error " . mysql_error() . "

";

}

break;

}

$rowCounter++;

}

echo "$rowCounter inserted.";

}

fclose($loadLocationCsvUrl);

}

processCSV();

?>

When I run my script I receive the following error before processing all rows of data.

maximum execution time of 30 seconds exceeded

After researching the problem, I should be able to extend the max_execution_time time which should resolve the problem.

But being in my PHP programming infancy I would like to know if there is a more optimal way of doing my script below, so I do not have to rely on "get out of jail cards".

The script is:

1 Taking a CSV file

2 Cherry picking some columns

3 Trying to insert 10k rows of CSV data into a my SQL table

In my head I think I should be able to insert in chunks, but that is so far beyond my skillset I do not even know how to write one line :\

Many thanks in advance

function processCSV()

{

global $uploadFile;

include 'dbConnection.inc.php';

dbConnection("xx","xx","xx");

$rowCounter = 0;

$loadLocationCsvUrl = fopen($uploadFile,"r");

if ($loadLocationCsvUrl <> false)

{

while ($locationFile = fgetcsv($loadLocationCsvUrl, ','))

{

$officeId = $locationFile[2];

$country = $locationFile[9];

$country = trim($country);

$country = htmlspecialchars($country);

$open = $locationFile[4];

$open = trim($open);

$open = htmlspecialchars($open);

$insString = "insert into countrytable set officeId='$officeId', countryname='$country', status='$open'";

switch($country)

{

case $country <> 'Country':

if (!mysql_query($insString))

{

echo "

error " . mysql_error() . "

";

}

break;

}

$rowCounter++;

}

echo "$rowCounter inserted.";

}

fclose($loadLocationCsvUrl);

}

processCSV();

?>

原文:https://stackoverflow.com/questions/6000416

更新时间:2020-01-03 14:33

最满意答案

首先,在2011年你不使用mysql_query。 您使用mysqli或PDO和准备好的语句。 然后,您不需要弄清楚如何转义SQL的字符串。 你使用了htmlspecialchars ,这是完全错误的。 接下来,您可以使用事务来加速许多插入。 MySQL还支持多种兴趣。

但最好的办法是使用CSV存储引擎。 http://dev.mysql.com/doc/refman/5.0/en/csv-storage-engine.html请阅读此处。 您可以立即将所有内容加载到SQL中,然后根据需要进行操作。 本文还介绍了load data infile命令。

First, in 2011 you do not use mysql_query. You use mysqli or PDO and prepared statements. Then you do not need to figure out how to escape strings for SQL. You used htmlspecialchars which is totally wrong for this purpose. Next, you could use a transaction to speed up many inserts. MySQL also supports multiple interests.

But the best bet would be to use the CSV storage engine. http://dev.mysql.com/doc/refman/5.0/en/csv-storage-engine.html read here. You can instantly load everything into SQL and then manipulate there as you wish. The article also shows the load data infile command.

2015-01-04

相关问答

我在view.php中给了SQL一个负索引,它失败了,但我解决了它。 I given negative index in the view.php to the SQL and it was failed it, but I sloved it.

您唯一的选择是增加脚本允许的执行时间(将其设置为0,使其无限制,但不推荐),或者产生一个新的线程,并希望获得最佳效果。 这是不可捕获的原因是它不是真的被抛出。 没有一行代码实际上触发了错误,而PHP表示,“不好意思,这太久了,现在关机了。 这是有道理的。 想象一下,一个脚本的最大执行时间为30秒,捕捉到这个错误,另外30秒钟在一个设计不善的程序中,打开了一些相当讨厌的机会来利用。 至少会为DOS攻击创造机会。 Your only options are to increase the allowe

...

你的循环可能是无止境的。 如果不是,您可以延长最长执行时间,如下所示: ini_set('max_execution_time', 300); //300 seconds = 5 minutes Your loop might be endless. If it is not, you could extend the maximum execution time like this: ini_set('max_execution_time', 300); //300 seconds = 5 mi

...

似乎更换了'localhost' <?php

$link = mysqli_connect('localhost', 'root', '', 'Bbase') or die('Error connecting to database');

?>

用'127.0.0.1'解决了这个问题,我不完全确定原因,但这样做后问题从未发生过。 It seems that Replacing 'localhost' in <?php

$link = mysqli_connect('localhost', '

...

首先,在2011年你不使用mysql_query。 您使用mysqli或PDO和准备好的语句。 然后,您不需要弄清楚如何转义SQL的字符串。 你使用了htmlspecialchars ,这是完全错误的。 接下来,您可以使用事务来加速许多插入。 MySQL还支持多种兴趣。 但最好的办法是使用CSV存储引擎。 http://dev.mysql.com/doc/refman/5.0/en/csv-storage-engine.html请阅读此处。 您可以立即将所有内容加载到SQL中,然后根据需要进行操作

...

这里以秒为单位设定时间限制。 设置-1为无限制的时间限制。 参数或者set_time_limit(500); Here set time limit which in seconds. Set -1 for unlimited time limit. set_time_limit(500);

如果您确定没有其他任何改变,现在发送电子邮件的时间是否可能需要30秒以上? 可以在PHP中调整30秒的最大值,可以在php.ini中全局调整,也可以按照请求调整。 虽然通常不建议在生产环境中增加此功能,但通常可以将后端作业(例如发送邮件)清除。 你可以这样做:ini_set('max_execution_time',600); 设置为0表示无限时间,但如果您在服务器上遇到实际问题,则您的请求可能无法完成。 If you are certain that nothing else has chang

...

我在一些内容丰富的PDF上也有这个问题。 默认情况下,PHP仅允许最长30秒的执行时间。 您可以通过更改以下行来增加php.ini文件中的时间: max_execution_time = 30

至 max_execution_time = 60

或者将其添加到脚本文件的顶部。 set_time_limit(60);

60秒应该足以让TCPDF做它需要做的事情,但你可能需要进一步增加它。 但要小心增加太多,因为它可能会导致问题。 注意:如果由于无限循环而达到最大执行时间,则不会改变任何内容。

...

如果在两台计算机上使用完全相同的代码,则问题可能是计算机的CPU /处理速度(不太可能)或代码处理的数据量(更有可能)。 但是,严格地说,如果您的代码达到了最大执行时间,那么您应该考虑减少代码在一次执行中的处理量。 如果构建代码来处理/处理大量请求,请考虑在一个请求中添加最大数量的数据以交错/批处理它们。 (例如,不要一次处理100行/请求,将上限设置为25并将其错开4次执行代码)。 如果这是一个大型流程,您可能希望考虑增加最大执行时间的上限。 If the exact same code is

...

尝试从php.ini文件增加max_execution_time 。 要么 在执行get_info()之前添加此行; function set_time_limit(60); Try increasing max_execution_time from php.ini file. OR add this line before executing your get_info(); function set_time_limit(60);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值