My csv export displaying html, how to get rid of?

后端 未结 3 862
悲&欢浪女
悲&欢浪女 2020-12-16 08:38

I\'ve seen this asked before and I am having trouble getting this to work properly after trying a number of solutions. The problem is I can\'t get my data to export into a c

3条回答
  •  情书的邮戳
    2020-12-16 09:08

    Got it working!

    I invoked my csv code before anything on the page. :) Then I did my connection to my table, then did my logic for my code. I didn't have an ob_start or ob_flush on my main file which made a big difference. I had the ob_clean before the while loop and then I did an exit() after declaring the header. Hopefully, this explains it well.

    Here is my code.

    if (isset($_POST["hidden"])) {
        $sql = "SELECT * FROM `newsletter`";
        $result = mysql_query($sql);
    
        ob_end_clean();
    
        $fp = fopen('php://output','w');
    
        while ($list = mysql_fetch_assoc($result)) {
            fputcsv($fp, $list);
        }
    
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=data.csv');
    
        exit();
    }
    

提交回复
热议问题