用PHP读取批量XML文件

时间:2016-02-15 06:43:01

标签: php xml xmlreader

我是PHP的新手。

我的客户共享了5GB XML产品信息数据。

我需要在DB中导入这些数据。

我使用了以下代码并在浏览器中运行。

<?php 
ini_set('max_execution_time', 0);
ini_set('memory_limit', '6144M');

$mysql_hostname = "localhost";
$mysql_user     = "XXXX";
$mysql_password = "XXXX";
$mysql_database = "XXXX";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");

$languages = simplexml_load_file("sample.xml");
$total_row =  count($languages->entry);
$data = $languages->entry;

foreach($data as $key => $value) {
    $title = $value->title; 
    $sql   = "INSERT INTO `YYYY` VALUES ( NULL, '$title')";
    $run = mysql_query($sql);  
}
echo "Completed ...... !";
?>

我收到以下错误消息。

error message

Apache错误日志:

[Mon Feb 15 12:24:40.140480 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00428: Parent: child process 5244 exited with status 3221225477 -- Restarting.
[Mon Feb 15 12:24:40.510501 2016] [ssl:warn] [pid 4752:tid 260] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:40.567504 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00455: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.5.30 configured -- resuming normal operations
[Mon Feb 15 12:24:40.567504 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00456: Apache Lounge VC11 Server built: Oct 13 2015 10:54:13
[Mon Feb 15 12:24:40.567504 2016] [core:notice] [pid 4752:tid 260] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Mon Feb 15 12:24:40.569505 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00418: Parent: Created child process 5032
[Mon Feb 15 12:24:40.946526 2016] [ssl:warn] [pid 5032:tid 272] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:41.091534 2016] [ssl:warn] [pid 5032:tid 272] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:41.121536 2016] [mpm_winnt:notice] [pid 5032:tid 272] AH00354: Child: Starting 150 worker threads. 

如何实现这个?

1 个答案:

答案 0 :(得分:0)

您可以提高性能并将所有查询传递给mysql一次:

function str_lreplace($search, $replace, $subject)
{
    $pos = strrpos($subject, $search);

    if($pos !== false)
    {
        $subject = substr_replace($subject, $replace, $pos, strlen($search));
    }

    return $subject;
}

$sql   = "INSERT INTO `YYYY` VALUES ";
foreach($data as $key => $value) {
    $title = $value->title; 
    $sql   =$sql. "( NULL, '$title'),";
    //$run = mysql_query($sql);  
}
//replace last occurrence of a  '),'  with  ');'  in php
$sql = str_lreplace('),',');', $sql);
$run = mysql_query($sql);  
echo "Completed ...... !";