Reputation: 738
I have looked at like 10 other posts with people who seem to have the same problem as me in, most suggest putting set_time_limit(0);
. But even with that no matter what script I run, always after around 9-10 minutes it just stops.
I have tried so many combinations of the below 3 variables, setting them to -1
, 0
, 9999999
; just always stops. It was not so long ago, I had the exact same script below run over night. And just one day I have this problem with any script.
I’m 100% sure it’s stopping before its finished, and it’s not stopping due to error. Because any of my scripts now stop at the same app run time.
Extremely frustrated, any help will be much appreciated.
In php.ini:
max_input_time = -1
max_execution_time = 0
set_time_limit = 0
Another scripts: (also stops after 10 minutes)
<?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";
?>
The script:
<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>
EDIT: No matter what script I run, or how simple it always stops after about 10 minutes. So I doubt it’s anything to do with the code in my script. So I'll add that my web server I’m running scripts on, is on Bluehost.
Upvotes: 4
Views: 1639
Reputation: 109
Sounds like your script is limited by the server. I think you can change this in php.ini.
Upvotes: 1
Reputation: 738
The answer is its a limitation bluehost has set to my account. Heres the response he gave me:
...
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
...
Upvotes: 7
Reputation: 26066
Some ideas.
First your code has these calls to a function called withslashes()
which I assume is a custom function designed to add slashes (aka: escape) values to quotes & other characters But you are using MySQLi calls, right? Then why not avoid reinventing the wheel and just use mysqli_real_escape_string()
which is part of the MySQLi extensions?
Also, I added mysqli_free_result()
which “Frees the memory associated with a result.” Which can’t hurt even if the result of your query is just an acknowledgement that the query ran.
And I added a sleep()
value of ` second to at least throttle the process a bit to give your server room to breath.
The whole goal is to use built-in PHP functions to avoid using more RAM than necessary when running MySQL queries & pausing the script by 1 second to give it breathing room.
Here is a refactored version of insertsentence()
using the first set of ideas to improve performance:
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;
}
}
But why not use mysqli_stmt_bind_param()
to make full use of the prepared statements capability of MySQLi? Then you no longer need to use mysqli_real_escape_string()
because prepared statements basically handle all of that.
With that in mind, here is another refactored version of insertsentence()
which uses 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;
}
}
Note the 'ssds'
in mysqli_stmt_bind_param()
. Each letter determines the types of your 4 values. So in this case it is string (sentence
), string (user
), integer (id
), string (language
) which is my best guesses based on the data you’re giving me.
Upvotes: 1