mysqli_fetch_row需要一个活动的DBConnect吗?

时间:2014-05-06 07:35:57

标签: php mysql

基本上我有一个脚本处理来自很多表的大量数据。它在完成之前停止并且我试图找出原因。所以我想知道是否像

这样的案例
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);
循环中的(做某事)不需要很长时间。但是循环在完成之前必须完成数万个循环。

行提取是否需要打开数据库连接?如果它确实可以,如果内部循环需要太长时间才能完成db连接超时?

我原本以为调用mysqli_fetch_rows会将非活动计时器重置为0.是吗?

这是真正的剧本:

<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>

0 个答案:

没有答案