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

148

I have a php script which inserts data into database table with a function call, and the function is in the for loop. I want 120+ iterations, but due max_execution_time=30 and unfortunately i cant change or edit php.ini file on my shared hosting. As the execution of the script stops at approx. 55-60 iterations of for loop. I was thinking that what if i include another php file like, include("script2.php"); can i continue the for loop from 61 and end at 120 in this script. So that i can complete all 120 iterations. Please correct me if i am wrong, i am new to 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();
    }
}
272

Answer

Solution:

Change the max execution time from within the script maybe:

ini_set("max_execution_time", $timeInSeconds);
273

Answer

Solution:

Use the following in your script:

set_time_limit($time);

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

The maximum execution time, in seconds. If set to zero, no time limit is imposed.

People are also looking for solutions to the problem: php - Do I always need to use mysql_fetch

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.