0

basically i have a script processing a lot of data from a lot of tables. And its stopping before its finished and im trying to find out why. So im wondering if a case such as

do
{
    $DBConnect = mysqli_connect("localhost", "username", "xxxxxxx!");
    mysqli_set_charset($DBConnect, "utf8");
    mysqli_select_db($DBConnect, "dbname");
    $qwry = "select something from `something`;"; // start writting all the  eng pages/
    $QueryResult = mysqli_query($DBConnect, $qwry); 
    $Row = mysqli_fetch_row($QueryResult);
    do
    {
    //.....(Doing something for a long time)...
    if(mysqli_ping($DBConnect)) $Row = mysqli_fetch_row($QueryResult);
    }while(something);

}while(somethingelse);

the (doing something) in the loop does not take a long time. but the loop has to do many tens of thousands of loops before its finished.

does the row fetching require the db connection to be open? if it does could the db connection time out if the inner loop takes too long to finish?

I would have thought that calling mysqli_fetch_rows would reset the inactive timer to 0. does it?

here is the real script in question:

<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";
$maxfiles = 1000;
$max = 10000;//49990;
$limit = "";//"limit 500";
$date = date("y-m-d");                         // 03.10.01
$date = "20".$date;
echo "date [$date]";
$start = '<?xml version="1.0" encoding="UTF-8"?>'."\n".'
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'."\n".'
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n".'
       xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\n".'
                           http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n";

        $stringData = '<?xml version="1.0" encoding="UTF-8"?>'."\n".'
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'."\n".'
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n".'
       xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\n".'
                           http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n".'
    <url>'."\n";

        $DBConnect = mysqli_connect("localhost", "user", "xxxxx!");
                    mysqli_set_charset($DBConnect, "utf8");
                    mysqli_select_db($DBConnect, "db");

            $qwry = "select ISO3 from `languages`;"; // start writting all the  eng pages/
        $QueryResult = mysqli_query($DBConnect, $qwry);                                     
        $Row = mysqli_fetch_row($QueryResult);
        $count = 0;
        do
        {
            if($Row[0] == 'yue') //just a language i want to skip from processing
            {
                $count--;
            }
            else $languages[$count] = $Row[0];

            $Row = mysqli_fetch_row($QueryResult);
            $count++;
        }while($Row);
        mysqli_close($DBConnect);
$lcount = 0;
ob_start();
do // language loop
{
$FileName = "sitemaps/".$languages[$lcount]."sitemap.xml";

$fh = fopen($FileName, 'w');// or die("can't open file"); // first erase the old sitemap
$stringData = "";
fwrite($fh, $stringData);
//echo "Last modified: " . date ("F d Y H:i:s.", getlastmod());
//exit();
$fh = fopen($FileName, 'a');// or die("can't open file"); // now make the new one


        fwrite($fh, $stringData);

        $DBConnect = mysqli_connect("localhost", "mrhowtos_user", "9Xhe9e949Xhe9e94!");
        mysqli_set_charset($DBConnect, "utf8");
        mysqli_select_db($DBConnect, "mrhowtos_main");

        $qwry = "select id, language from `sentences` order by id ASC $limit;"; // start writting all the  eng pages/
        $QueryResult = mysqli_query($DBConnect, $qwry);                                     
        $Row = mysqli_fetch_row($QueryResult);

$filecount = 0;
$count = 1; 
$tcount = 0;

do // file loop
{               $skipcount = 0;
    do// sentence loop
    {

        if(($Row[1] != 'yue') and ($Row[1] != $languages[$lcount]) and (hastranslationsin($Row[0], $languages[$lcount])) ) //if languages dont match, and a translation in the desired lang was found
        {
                $stringData = ' <url>'."\n".'
                <loc>http://www.mrhowtosay.com/view?id='.$Row[0].'&amp;lang='.$languages[$lcount].'</loc>'."\n".'
                <lastmod>'.$date.'T14:22:08+00:00</lastmod>'."\n".'
                <priority>0.80</priority>'."\n".'
                <changefreq>monthly</changefreq>'."\n".'
                </url>'."\n";
                fwrite($fh, $stringData);   
                echo 'http://www.mrhowtosay.com/view?id='.$Row[0].'&amp;lang='.$languages[$lcount].'<br />';
            $count++;
            $tcount++;      
        }


        ob_flush();
        flush();
        $Row = mysqli_fetch_row($QueryResult);

    }while(($Row) and ($count < $max));

    $stringData = '</urlset>'; // 
    fwrite($fh, $stringData); // print last line of the file
    fclose($fh);
    echo "1 sitemap Finished!".$count." URLs inserted into the sitemap $FileName in main dir<br />";

    if($Row) // prepare next file for writting, 
    {
        $count = 0; // reset the to 50,000 counter for next file
        $FileName = "sitemaps/".$languages[$lcount]."sitemap".$filecount.".xml";//define new file name
        //make the new file
        $newfile = fopen("./$FileName", "w");
        fclose($newfile);

        $fh = fopen($FileName, 'w') ;//or die("can't open file"); // first erase the old sitemap
        $stringData = "";//make the starting declaration of the sitemap
        fwrite($fh, $stringData); // 
        $fh = fopen($FileName, 'a') ;//or die("can't open file"); // now make the new one
        fwrite($fh, $start);
        $filecount++;
    }

}while(($Row) and($filecount < $maxfiles)); 

$filestotal = ($filecount+1);   
$filecount++;
$filestotal = $filestotal + $filecount;
echo "finished language [".$languages[$lcount]."] $filestotal sitemaps created, and total of $tcount urls<br />";

if ($DBConnect) mysqli_close($DBConnect);
$lcount++;
}while($languages[$lcount]);
echo "finished all sitesmaps for all ".($lcount)." languages<br />";
?>
</body>
</html>
user1397417
  • 708
  • 4
  • 11
  • 34
  • Yes it requires an open db connection. If the connection has timed out your script will fail. I have run in to the same issues with aggregation jobs that takes a long time. You can look into the mysqli_ping function – Jesper Blaase May 06 '14 at 07:40
  • How does it fail? The issue may be too short max execution time for operation in your php server. – Doro May 06 '14 at 07:42
  • well im happy i found the problem anyway. what is the usual workaround? can i refresh the connection? or do some arbitrary qwry once in a while to keep the connection alive? – user1397417 May 06 '14 at 07:43
  • i dont really know why it stops, the display turns to connection was reset after like 5 minutes, but the process continues to write to file. and then eventually just stops long before its done. if its possible for a timeout to occur while im fetching rows then i think thats whats happeneing – user1397417 May 06 '14 at 07:51
  • You have to set max execution time for php scripts. http://stackoverflow.com/questions/16171132/how-to-increase-maximum-execution-time-in-php – Doro May 06 '14 at 07:55
  • i have set_time_limit(0); in my script already. i had a different script run for 2 days a couple days ago. – user1397417 May 06 '14 at 08:01
  • im trying now with the mysqli_ping on the fetch row (see update) so far it has not stopped, not sure if its been the amount of time it normally stops thought. – user1397417 May 06 '14 at 08:21
  • hmm nope it still stops after about 10 minutes – user1397417 May 06 '14 at 08:22
  • mysqli.reconnect was set to Off in php.ini set it to On and retrying... – user1397417 May 06 '14 at 08:30
  • nope still stops after about 10 minutes – user1397417 May 06 '14 at 09:05

0 Answers0