2012-11-15 53 views
0

我一直在嘗試從ascii version of the data將美國農業部營養數據加載到mysql數據庫中。我使用了here中的腳本,除了大小爲34MB的NUT_DATA.txt文件外,其他所有內容都進來了。該文件使用PHP腳本正確加載(因此內存限制不應該是一個問題,它們被設置爲128M反正)使用file_get_contents函數。但是,當它到達preg_split('#\n#', $file)行時,腳本無需進一步評論就結束。如果我把文件分成更小的部分,它運行良好。爲什麼它不能在完整的文件上工作?我將memory_limit設置爲128M,將upload_max_filesize設置爲128M。整個文件似乎已被讀入,這只是preg_split,似乎有問題。php preg_split似乎在大字符串上崩潰

有什麼明顯的我失蹤了嗎?

謝謝!

這裏是PHP代碼我一直在測試:

<?php 
// from http://drupal.org/node/107806 

//$dbh=mysql_connect(/*db info*/); 
//mysql_select_db(/*db info*/); 
try 
{ 
    $dbh = new PDO('mysql:host=127.0.0.1;dbname=XXXXXXX', 'XXXXXXX', 'XXXXXXX'); 
    echo "PDO access worked! <br />"; 
} catch (PDOException $e) 
{ 
    echo "PDO error: " . $e->getMessage() . "<br />"; 
    die(); 
} 

//List of SR20 filenames with associated field counts. 
$filenames = array(//"DERIV_CD" => 2, 
//  "FD_GROUP" => 2, 
//  "NUTR_DEF" => 6, 
//  "SRC_CD" => 2, 
//  "WEIGHT" => 7, 
//  "FOOTNOTE" => 5, 
//  "DATA_SRC" => 9, 
//  "DATSRCLN" => 3, 
//  "FOOD_DES" => 14, 
     "NUT_DATA" => 17 
//  "NUT_DATA2" => 17, 
//  "NUT_DATA_test" => 17 
    ); 
foreach($filenames as $filename => $count) { 
    echo "inside foreach: $filename "; 
    $file = file_get_contents($filename.'.txt'); 
    echo " read in the file, length = " . strlen($file) . "<br />"; 
    $lines = preg_split('#\n#', $file); 
    echo "made it past preg_split <br />"; 
// print_r($lines); 
    echo " lines = " . count($lines). "<br />"; 
    for($i=0;$i<count($lines);++$i) { 
     $line = $lines[$i]; 
//  echo "Inside for loop, line = '" . $line . "'<br />"; 
     //Some text fields are split over several lines. Concatenate them. 
     while(substr_count($line, '^') < $count-1 || (substr_count($line, '~')%2) == 1) { 
      ++$i; 
      if($i>=count($lines)) { break; } 
      $line .= '<br />'.$lines[$i]; 
     } 
     $fields = trim($line); 
     if(strlen($fields) == 0) continue; 
     $fields = str_replace(array("'", '~', '^'), array("''", "'", ','), $fields); 
     $sql = "INSERT INTO `$filename` VALUES($fields);"; 
     //Insert zeroes for unfilled values. 
     $sql = str_replace(array(',,', ',)'), array(',0,', ',0)'), $sql); 
     $sql = str_replace(array(',,', ',)'), array(',0,', ',0)'), $sql); 
     $sql = str_replace(array(',,', ',)'), array(',0,', ',0)'), $sql); 
     //Some single-char fields aren't quoted. 
     $sql = preg_replace('#,([A-Z])\);$#', ",'$1');", $sql); 
// comment out the database interface until things are working 
/*  $stmt = $dbh->prepare($sql); 
     if(!$stmt->execute()) 
     { 
      echo $sql; 
     } 
*/ 
    } 
} 
echo "done"; 
?> 

回答

0

添加以下兩行到我的PHP的頂部:

ini_set('display_errors', 'On'); 
error_reporting(E_ALL | E_STRICT); 

,它告訴我,它已經運行內存不足。再次提高內存(至256M)解決了這個問題。我想我無法猜測一個任務要使用多少內存。

-1

添加這個配置到你的httpd.conf

ThreadStackSize 8388608