2015-09-09 30 views
0

我正在使用此簡單代碼來讀取80 MB .mdb文件並將其轉換爲WAMP環境中的CSV,但出現了令人驚訝的高內存使用情況(超過512 MB)在轉儲訪問表時減少內存使用情況

是否有任何方法來拆分加載或其他方式來避免如此高的內存使用量?

$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password); 

$qry = "SELECT * FROM data"; 

$result = odbc_exec($conn,$qry); 

$theArray = array(); 

while (($row = odbc_fetch_array($result))) 
    { 
     array_push($theArray, $row); 
    } 

$fp = fopen('dispo_e.csv', 'w'); 
foreach ($theArray as $lines) 
{ 
    fputcsv($fp, $lines, ";"); 
} 
+0

如果你喜歡的任何答案,確保你投票了,點擊你喜歡的旁邊的機票*(無恥的自我推銷)* –

回答

2

最簡單的選擇是不將它分配給數組,而是直接寫入CSV。

$conn = odbc_connect(
    "Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", 
    $user, 
    $password 
); 

$result = odbc_exec($conn, 'SELECT * FROM data'); 

$fp = fopen('dispo_e.csv', 'w'); 
while (($row = odbc_fetch_array($result))) { 
    fputcsv($fp, $lines, ';'); 
} 

fclose($fp); 
-1

你可以試試這個功能,而不是(以下使用):

<?php 
    function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true) { 

     if($attachment) { 
      // send response headers to the browser 
      header('Content-Type: text/csv'); 
      header('Content-Disposition: attachment;filename='.$filename); 
      $fp = fopen('php://output', 'w'); 
     } else { 
      $fp = fopen($filename, 'w'); 
     } 

     $result = mysql_query($query, $db_conn) or die(mysql_error($db_conn)); 

     if($headers) { 
      // output header row (if at least one row exists) 
      $row = mysql_fetch_assoc($result); 
      if($row) { 
       fputcsv($fp, array_keys($row)); 
       // reset pointer back to beginning 
       mysql_data_seek($result, 0); 
      } 
     } 

     while($row = mysql_fetch_assoc($result)) { 
      fputcsv($fp, $row); 
     } 

     fclose($fp); 
    } 

    // Using the function 
    $sql = "SELECT * FROM table"; 
    // $db_conn should be a valid db handle 

    // output as an attachment 
    query_to_csv($db_conn, $sql, "test.csv", true); 

    // output to file system 
    query_to_csv($db_conn, $sql, "test.csv", false); 
?> 
+0

所以你剛剛複製了一塊你有的代碼!這與OP的問題有什麼關係?沒有!你應該回答這個問題,而不是改變問題以適應你認爲你可能有的答案。 – RiggsFolly