简体   繁体   中英

500 Internal Server Error - Due to timing out?

I have a php page that runs a series of queries and loops as shown below, but I am getting a 500 Internal Server Error which I can only assume is down to a timing out.

The main loop should loop through about 3000 records but I have added the if statement to check if it works with a lower number which it does (the userIDs are 1, 2, 3, 4 etc and therefore indicate the number of records being looped through). Up this number to 2000 and I get the error after about 1 minute.

ini_set('max_execution_time', 9000); 
...
$sqlSelect = "SELECT DISTINCT userID FROM tblResults;";
$selectedIDs = mysql_query($sqlSelect);

while($resultRow = mysql_fetch_array($selectedIDs)) {
    $userID = $resultRow['userID'];
    set_time_limit(0);

    if ($userID < 500) {
        ....
        series of queries and loops
        ....
    }
}

I have tried setting the max_execution_time and have put set_time_limit(0); at the beginning of any loop but this does not seem to help. I can't find any errors in any of logs relating to the error.

Also, it is not an issue with the data as if I try:

    if ($userID >= 500 && $userID < 1000) 
    if ($userID >= 1000 && $userID < 1500) 
    if ($userID >= 1500 && $userID < 2000)   etc 

it all works. But try:

    if ($userID < 2000) [ie the same data but all at once]

if fails.

Any thoughts?

As a DBA I belive it to be wise to move some of the logic to the SQL instead. Since you don't specify the rest of the queries the best option could be:

SELECT DISTINCT userID FROM tblResults where userID < 500

Combine this with the rest of your queries I bet the execution time should decrease. Since > 1 minute execution for 2000 iterations seems a bit long, suggesting you have some nasty stuff going on in that loop :)

Would you try to run this code ?

while($resultRow = mysql_fetch_object($selectedIDs)) {
        $userID = $resultRow->UserID;

        if ($userID < 500) {
            ....
            series of queries and loops
            ....
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM