2017-06-15 85 views
0

當我有這個php功能:資源ID#13試圖查詢結果導出爲CSV

function get_download($dbmi, $getid) { 

    $stmt = $dbmi->prepare("CALL spStartDownload(?)"); 
    $stmt->bind_param('i', $getid); 
    $stmt->execute(); 
    $result = $stmt->get_result(); 

    $fp = fopen('file.csv', 'w'); 
    while($row = $result->fetch_assoc()) { 
     fputcsv($fp, $row); 
    } 

    fclose($fp); 

    $stmt->close(); 

    echo $fp; 
} 

我想不通爲什麼它給我的資源ID#13錯誤。

任何幫助,非常感謝。

回答

0

我解決了這個使用這個新功能:

function get_download($dbmi, $getid) { 

    $stmt = $dbmi->prepare("CALL spStartDownload(?)"); 
    $stmt->bind_param('i', $getid); 
    $stmt->execute(); 
    $result = $stmt->get_result(); 

    $fp = fopen('php://output', 'w'); 
    if ($fp && $result) { 
     header('Content-Type: text/csv'); 
     header('Content-Disposition: attachment; filename="export.csv"'); 

     fputcsv($fp, $headers, ";"); 
     while($row = $result->fetch_assoc()) { 
      fputcsv($fp, $row, ";"); 
     } 
    } 

    $stmt->close(); 

    echo $fp; 
}