T
T
Teraxis2017-09-08 21:44:49
PHP
Teraxis, 2017-09-08 21:44:49

Why does while in php behave like this?

There is such a php function that processes a huge xml (1.5 GB, 1.5 million records in child tags).
xml example

<?xml version="1.0" encoding="utf-8"?>
<DATA FORMAT_VERSION="1.0">
<RECORD><NAME>ТОВАРИСТВО З ОБМЕЖЕНОЮ ВІДПОВІДАЛЬНІСТЮ ВИРОБНИЧО-ТОРГОВА ФІРМА ШАРМ</NAME><SHORT_NAME>ТОВ &quot;ВТФ &quot;ШАРМ&quot;</SHORT_NAME><EDRPOU>00308749</EDRPOU><ADDRESS>93113, Луганська обл., місто Лисичанськ, ПРОСПЕКТ ЛЕНІНА, будинок 159</ADDRESS><BOSS>ОЛІЙНИК ТЕТЯНА САВЕЛІЇВНА</BOSS><KVED>14.13 Виробництво іншого верхнього одягу</KVED><STAN>зареєстровано</STAN><FOUNDERS><FOUNDER>БАТУРКІНА ІРИНА МИХАЙЛІВНА, розмір внеску до статутного фонду - 3000.00 грн.</FOUNDER><FOUNDER>РЕВА ВАЛЕНТИНА ІВАНІВНА, розмір внеску до статутного фонду - 3000.00 грн.</FOUNDER><FOUNDER>ПЕТРЕНКО КЛАВДІЯ СЕРГІЇВНА, розмір внеску до статутного фонду - 3000.00 грн.</FOUNDER></FOUNDERS></RECORD>
</DATA>

xml handler
function updateFirmsXML()
    {
        $mysql = connectBase();
        $dir = __DIR__ . '/../../tmp/';
        $xmlURL = $dir . '15.1-EX_XML_EDR_UO.xml';
        $xml = new XMLReader();
        $xml->open($xmlURL);

        $start_time = time();
        header('Content-Type: text/html; charset=utf-8');

        $count = 0;
        $count2 = 0;
        while($xml->read())
        {

            if($xml->nodeType == XMLReader::ELEMENT && $xml->name == 'RECORD')
            {
                $item[] = "('firms', '" . mysqli_real_escape_string($mysql, $xml->readOuterXML()) . "')";
            }

            if($xml->nodeType == XMLReader::END_ELEMENT && $xml->name == 'RECORD')
            {
                $count++;
                $count2++;
                if ($count >= 500) {
                    saveXMLtoDB($item);
                    $item = array();
                    $count = 0;
                }
            }
        }

        $xml->close();
        echo saveXMLtoDB($item);
        die;
  }

Add to base function
function saveXMLtoDB($item = false){
        $mysql = connectBase();
        $sql = "INSERT INTO _parse_tmp (parse_key, parse_value) VALUES " . implode(", ", $item);
        mysqli_query($mysql, $sql);
        echo 'Success';
    }

There are 1,547,772 RECORD records in the database. If we remove echo 'Success' from the saveXMLtoDB function, more than 9 million records are added to the database. With echo 'Success' everything works fine. If you limit the addition of only the first, for example, 1000 records, then everything also works fine.
Who can tell why this is happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question