2017-06-15 88 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; 
}