我可以包含另一个 php 脚本来继续执行 for 循环以防止服务器因max_execution_time超时吗?


Can i include another php script to continue the for loop execution to prevent server timeout due max_execution_time

我有一个php脚本,它通过函数调用将数据插入数据库表,并且该函数处于for循环中。我想要 120+ 次迭代,但由于 max_execution_time=30,不幸的是我无法在我的共享主机上更改或编辑 php.ini 文件。由于脚本的执行在大约 for 循环的 55-60 次迭代处停止。我在想,如果我包含另一个 php 文件怎么办, 包括("脚本2.php");我可以在这个脚本中继续从 61 到 120 结束的 for 循环吗?这样我就可以完成所有 120 次迭代。如果我错了,请纠正我,我是 php 的新手。

if (isset($_POST["roll"])&&isset($_POST["resultType"])){
    global $result;
    global $t;
    $result=new Parser($_POST["resultType"]);
    $n = $_POST["roll"];
    $t = substr($n, 0, strlen($n) - 3);
    $con=mysqli_connect("localhost","root","","rolltu");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    // Create table
    $sql="CREATE TABLE $t(NAME CHAR(30),ROLL CHAR(30) NOT NULL,PRIMARY KEY(roll),Marks INT)";
    // Execute query
    if (mysqli_query($con,$sql))
    {
        echo "Table $t created successfully. <br>";
    }
    else
    {
        echo "Error creating table: " . mysqli_error($con);
        echo "<br>";
    }
    $arr = array($t,'000');
    $s = join("",$arr);
    $q ="$s";
    $c = 120;
    for($i = 0;$i <$c;$i++) {
    $q++;
    $result->requestResult($q);
    showResult();
    }
}

从脚本中更改最大执行时间:

ini_set("max_execution_time", $timeInSeconds);

在脚本中使用以下命令:

set_time_limit($time);

http://php.net/manual/en/function.set-time-limit.php

最长执行时间(以秒为单位)。如果设置为零,则不施加时间限制。