使用PHP将MySQL数据下载到CSV文件并返回整个网页

使用PHP将MySQL数据下载到CSV文件并返回整个网页,php,mysql,csv,download,Php,Mysql,Csv,Download,我下载的CSV文件似乎包含了整个网页代码,有人能看到我哪里出错了吗 最初它是在函数调用之后编写所有内容,但通过使用exit,我能够解决这个问题 $filename = 'SalesReport-'.date("Y-m-d H:i:s").'.csv'; // open raw memory in write mode as file so no temp files needed $fp = fopen('php://output', "w");

我下载的CSV文件似乎包含了整个网页代码,有人能看到我哪里出错了吗

最初它是在函数调用之后编写所有内容,但通过使用exit,我能够解决这个问题

        $filename = 'SalesReport-'.date("Y-m-d H:i:s").'.csv';
        // open raw memory in write mode as file so no temp files needed
        $fp = fopen('php://output', "w");

        $row = mysqli_fetch_assoc($sql);

        $seperator = "";
        $comma = "";

        //write in table headers
        foreach($row as $name => $value) {
            $seperator .= $comma . '' .str_replace('','""',$name);
            $comma = ",";
        }
        $seperator .= "\n";
        fputs($fp, $seperator);

        //skip past the 0th value
        mysqli_data_seek($sql,0);

        //write in the table data
        while($row = mysqli_fetch_assoc($sql)){
            $seperator = "";
            $comma = "";

            foreach($row as $name => $value) {
        //take any commas out initially which would confuse the csv file
                $value = str_replace(",", " ", $value);
                $seperator .= $comma.''.str_replace('','""',$value);
                $comma = ",";
            }
            $seperator .= "\n";
            fputs($fp, $seperator);
        }

        // tell the browser it's going to be a csv file
        header('Content-Type: application/csv;');
        // tell the browser to save it instead of displaying it
        header('Content-Disposition: attachement; filename="'.$filename.'";');
        fclose($fp);
        exit();

你说的整个网页是什么意思?如果你有Yo,那么你将得到整个网页…它返回我为整个网页编写的所有代码。我只希望表格在下载的文件中?已解决:只需要从ob_clean开始;