即使使用set_time_limit(0),php脚本也会停止

时间:2014-05-12 06:19:41

标签: php mysql

我看过其他10个帖子,其中的人似乎与我有同样的问题,大多数建议使用set_time_limit(0);。但即便如此,无论我运行什么脚本,总是在大约9-10分钟之后它就会停止。

我已经尝试了以下3个变量的这么多组合,将它们设置为-109999999;总是停下来。不久前,我在下面运行了完全相同的脚本。只有一天,我对任何脚本都有这个问题。

我100%确定它在完成之前就已经停止了,并且由于错误而没有停止。因为我的任何脚本现在停止在相同的应用程序运行时。

非常沮丧,任何帮助将不胜感激。

在php.ini中:

max_input_time = -1
max_execution_time = 0
set_time_limit = 0

另一个脚本:(也会在10分钟后停止)

<?php
set_time_limit(0);
ignore_user_abort(true);
function categoryinsert($english, $name, $keywords, $language)
{
    if(!$english)
    {
        echo "no english recieved! in categoryinsert<br />";
        return 0;
    }
    else if(!$name)
    {
        echo "no name recieved! in categoryinsert<br />";
        return 0;
    }
    else if(!$language)
    {
        echo "no language recieved! in categoryinsert<br />";
        return 0;
    }
    $DBConnect = mysqli_connect("localhost", "USER***", "************");
    mysqli_set_charset($DBConnect, "utf8");
    mysqli_select_db($DBConnect, "db***");

    $qwry = "INSERT INTO `categories` (english, name, keywords, language) values ('$english','$name','$keywords','$language');";
            $QueryResult = mysqli_query($DBConnect, $qwry);
            //Or die("<p>Unable to execute the query.[".$qwry."]<p>"
            //. "<p>Error code " . mysqli_errno($DBConnect)
            //. ": " . mysqli_error($DBConnect)) . "</p>"; 

            mysqli_close($DBConnect);
}
function checkfor($english, $lang)
{
    $DBConnect = mysqli_connect("localhost", "USER***", "************");
    mysqli_set_charset($DBConnect, "utf8");
    mysqli_select_db($DBConnect, "db***");

    $qwry = "SELECT * FROM `categories` where english = '$english' and language = '$lang';";
            $QueryResult = mysqli_query($DBConnect, $qwry);

            $Row = mysqli_fetch_row($QueryResult);

            mysqli_close($DBConnect);

            if($Row) return true;
            else return false;
}
function categoryupdate($keywords, $language, $english)
{
if(!$english)
    {
        echo "no english recieved! in categoryupdate<br />";
        return 0;
    }
    else if(!$keywords)
    {
        echo "no keywords recieved! in categoryupdate<br />";
        return 0;
    }
    else if(!$language)
    {
        echo "no language recieved! in categoryupdate<br />";
        return 0;
    }
    $DBConnect = mysqli_connect("localhost", "USER***", "************");
    mysqli_set_charset($DBConnect, "utf8");
    mysqli_select_db($DBConnect, "db***");

    $qwry = "UPDATE `categories` set keywords = '$keywords' where language = '$language' and language = '$language';";
            $QueryResult = mysqli_query($DBConnect, $qwry)
            Or die("<p>Unable to execute the query.[".$qwry."]<p>"
            . "<p>Error code " . mysqli_errno($DBConnect)
            . ": " . mysqli_error($DBConnect)) . "</p>"; 

            mysqli_close($DBConnect);
}
function translatekeywords($keywords, $tolang)
{

    if(!$keywords)
    {
        echo "no keywords recieved! in translatekeywords<br />";
        return 0;
    }
    else if(!$tolang)
    {
        echo "no tolang recieved! in translatekeywords<br />";
        return 0;
    }
    $parts = explode(", ", $keywords);
    $count = 0;
    $out = "";

    while($parts[$count])
    {
        if(($count != 0) and ($result)) $out = $out . ", ";
        $result = translate($parts[$count], 'eng', $tolang);
        if($result) $out = $out . $result;
        $count++;
    }

    return $out;
}
include '../functions.php';

$DBConnect = mysqli_connect("localhost", "USER***", "************");
    mysqli_set_charset($DBConnect, "utf8");
    mysqli_select_db($DBConnect, "db***");


            $qwry = "SELECT english, keywords FROM `categories` where language = 'eng' order by name ASC;";
            $QueryResult = mysqli_query($DBConnect, $qwry)
            Or die("<p>Unable to execute the query.[".$qwry."]<p>"
            . "<p>Error code " . mysqli_errno($DBConnect)
            . ": " . mysqli_error($DBConnect)) . "</p>"; 

            $count = 0;
            $Row = mysqli_fetch_row($QueryResult);

            do
            {
                $categories[$count] = $Row;             
                echo $count.') ['.$categories[$count][0].']['.$categories[$count][1].']<br />';
                $Row = mysqli_fetch_row($QueryResult);
                $count++;
            }while($Row);

            $qwry = "SELECT ISO3 FROM `languages` order by name ASC;";
            $QueryResult = mysqli_query($DBConnect, $qwry)
            Or die("<p>Unable to execute the query.[".$qwry."]<p>"
            . "<p>Error code " . mysqli_errno($DBConnect)
            . ": " . mysqli_error($DBConnect)) . "</p>"; 

            $count = 0;
            $Row = mysqli_fetch_row($QueryResult);

            do
            {
                $languages[$count] = $Row[0];
                $Row = mysqli_fetch_row($QueryResult);
                echo '['.$languages[$count].']<br />';
                $count++;
            }while($Row);

            $lcount = 0;
            do
            {
                if($languages[$lcount] != 'eng')
                {
                    $ccount = 0;
                    do
                    {
                        if(!checkfor($categories[$ccount][0], $languages[$lcount]))
                        {
                            $name = translate($categories[$ccount][0], 'eng', $languages[$lcount]);
                            if($categories[$ccount][1]) $keywords = translatekeywords($categories[$ccount][1],$languages[$lcount]);
                            categoryinsert($categories[$ccount][0], $name, $keywords, $languages[$lcount]);
                        }
                        $ccount++;
                    }while($categories[$ccount]);
                }

            $lcount++;
            }while($languages[$lcount]);

mysqli_close($DBConnect);
echo "FINISHED! [$lcount] languages proccessed";
?>

剧本:

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body>
<?php 
set_time_limit(0);
ignore_user_abort(true);
include 'functions.php'; 

function insertsentence($sentence, $lang, $id, $user)
{
    if(($lang == 'epo') and (strlen($sentence) < 255) )
    {
        $DBConnect = mysqli_connect("localhost", "xxxxx_userx", "xxxxxxx!");
        mysqli_set_charset($DBConnect, "utf8");
        mysqli_select_db($DBConnect, "xxxxx_main");

            $insertqwry = "INSERT INTO `sentences` (sentence, user, id, language) VALUES ('".withslashes($sentence)."', '".withslashes($user)."', '".withslashes($id)."', '".withslashes($lang)."');";

        $QueryResult = mysqli_query($DBConnect, $insertqwry) ;
        //Or die("<p>Unable to execute the query.[".$insertqwry."]<p>"
        //. "<p>Error code " . mysqli_errno($DBConnect)
        //. ": " . mysqli_error($DBConnect)) . "</p>"; 

        mysqli_close($DBConnect);
return 1;
    }

}

$myFile = "sentences_detailed.csv"; 
$fh = fopen($myFile, 'r');
$s = fread($fh, 3);
if ($s != pack('CCC',0xef, 0xbb, 0xbf)) {
    // bom not found, rewind file
    fseek($fh, 0, SEEK_SET);
}

$count = 0;
ob_start();
do
{

    $line = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', withslashes(trim(fgets($fh))));
    $parts = explode("  ", $line);
        $id = $parts[0];
        $lang = $parts[1];
        $sentence = withslashes($parts[2]);
        $user = withslashes($parts[3]);
        $note = "";


        if ((!$line) or ($line == "") or ($line == "!"))
        {
            echo ($count-1)." entries were processed<br />";
            echo "done<br />";
            exit();
        }



                if ($sentence != "!" )
                {
                    if (insertsentence($sentence, $lang, $id, $user))
                    echo "!";
                }

        ob_flush();
        flush();

    $count++;

    echo ".";

}while($line);

fclose($fh);
mysqli_close($DBConnect);

echo ($count-1)." entries were processed<br />";

?>
</body>
</html>

编辑:无论我运行什么脚本,或者大约10分钟后它总是停止多么简单。所以我怀疑这与我脚本中的代码有什么关系。因此,我将添加我正在运行脚本的Web服务器,位于Bluehost

4 个答案:

答案 0 :(得分:7)

答案是bluehost设置为我的帐户的限制。继承了他给我的回应:

...
The Dedicated IP (+$3.33 per month) will increase the limits on the account.


    The following it the limits on how the server will kill processes with and without the Dedicated IP on the account: 

                                    With a Dedicated IP         Without Dedicated IP
Apache/Web (HTML/PHP/Downloads)     12 hours                    10 minutes
...

答案 1 :(得分:1)

考虑使用PHP的flush函数,这样你可以连续执行你的循环 - 我知道这不是最好的也是最终的解决方案,但这可能对你有帮助:)(/ p>

首先查看此example

答案 2 :(得分:1)

一些想法。

首先,您的代码对名为withslashes()的函数进行了这些调用,我假设这是一个自定义函数,旨在将斜杠(aka:escape)值添加到引号和&amp;其他字符但你正在使用MySQLi调用,对吧?那么为什么不避免重新发明轮子而只使用mysqli_real_escape_string()这是MySQLi扩展的一部分?

此外,我添加了mysqli_free_result()“释放与结果相关联的内存。”即使查询结果只是确认查询已运行,​​也不会有任何损害。

我添加了sleep()的第二个值,至少可以稍微限制一下这个过程,让你的服务器空间能够呼吸。

整个目标是使用内置的PHP函数来避免在运行MySQL查询时使用比所需更多的RAM。将剧本暂停1秒钟以使其有喘息空间。

以下是insertsentence()的重构版本,其中使用了第一组提高效果的提示:

function insertsentence($sentence, $lang, $id, $user) {

    if (($lang == 'epo') and (strlen($sentence) < 255)) {
        $DBConnect = mysqli_connect("localhost", "xxxxx_userx", "xxxxxxx!");
        mysqli_set_charset($DBConnect, "utf8");
        mysqli_select_db($DBConnect, "xxxxx_main");

        $insertqwry = "INSERT INTO `sentences` (sentence, user, id, language)"
                    . " VALUES ('$sentence', '$user', '$id', '$lang');"
                    ;

        // Escape the query.
        $insertqwry = mysqli_real_escape_string($DBConnect, $insertqwry);

        $QueryResult = mysqli_query($DBConnect, $insertqwry) ;

        // Free the result set.
        mysqli_free_result($QueryResult);

        mysqli_close($DBConnect);

        // Sleep for 1 second.
        sleep(1);

        return TRUE;
    }
}

但为什么不使用mysqli_stmt_bind_param()来充分利用MySQLi的预处理语句功能呢?然后你不再需要使用mysqli_real_escape_string(),因为预备语句基本上可以处理所有这些。

考虑到这一点,这是使用insertsentence()的另一个重构版mysqli_stmt_bind_param()

function insertsentence($sentence, $lang, $id, $user) {

    if (($lang == 'epo') and (strlen($sentence) < 255)) {
        $DBConnect = mysqli_connect("localhost", "xxxxx_userx", "xxxxxxx!");
        mysqli_set_charset($DBConnect, "utf8");
        mysqli_select_db($DBConnect, "xxxxx_main");

        $insertqwry = "INSERT INTO `sentences` (sentence, user, id, language)"
                    . " VALUES (?, ?, ?, ?);"
                    ;

        // Bind the values to the statement.
        mysqli_stmt_bind_param($insertqwry, 'ssis', $sentence, $user, $id, $lang);

        $QueryResult = mysqli_query($DBConnect, $insertqwry) ;

        // Free the result set.
        mysqli_free_result($QueryResult);

        mysqli_close($DBConnect);

        // Sleep for 1 second.
        sleep(1);

        return TRUE;
    }
}

请注意'ssds'中的mysqli_stmt_bind_param()。每个字母确定您的4个值的类型。所以在这种情况下它是字符串(sentence),字符串(user),整数(id),字符串(language)这是我根据数据的最佳猜测你在给我。

答案 3 :(得分:1)

听起来您的脚本受到服务器的限制。我想你可以在php.ini中改变它。