Search code examples
phpmysqltimeout

How to keep a php script from timing out because of a long mysql query


I have an update query being run by a cron task that's timing out. The query takes, on average, five minutes to execute when executed in navicat.

The code looks roughly like this. It's quite simple:

// $db is a mysqli link
set_time_limit (0); // should keep the script from timing out
$query = "SLOW QUERY";
$result = $db->query($query);
if (!$result)
    echo "error";

Even though the script shouldn't timeout, the time spent waiting on the sql call still seems to be subject to a timeout.

Is there an asynchronous call that can be used? Or adjust the timeout?

Is the timeout different because it's being called from the command line rather than through Apache?

Thanks


Solution

  • I had the same problem somwhere, and "solved" it with the following code (first two lines of my file):

    set_time_limit(0);
    ignore_user_abort(1);