网页代码写入.csv而不是查询结果。 错误

Why is this script writing the full PHP page code (css/html) to the .csv instead of my query results? I suspect it has something to do with the COM ADODB connection / "File reference for download.". I would change the connection type, but the COM connection seems to be required via colleague communications.

  • Using PHP with COM ADODB Microsoft Access Driver

    $db = $_SERVER["DOCUMENT_ROOT"]."/CS_Sites.accdb";
    $conn = new COM("ADODB.Connection") or die("Cannot start ADO");
    $conn->open("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$db");
    
    $SRes_Q = $_POST["SiteResponsibilityQuery"];    
    
    $query = "SELECT [Site Name]";
    $query .= "FROM [Site Prioritization Factors]";
    $query .= "WHERE [Site Responsibility]= '$SRes_Q'";
    $rs = $conn->execute($query);
    
    //File reference for download.
    $filename = "CS_Site_Query_" . date('Ymd') . ".csv";
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: text/csv");
    $out = fopen('php://output', 'w');
    
    foreach($rs as $fields){
     fputcsv($out, $fields); 
    }
    fclose($out);