Export CSV from Mysql

前端 未结 5 1124
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 06:04

I\'m having a bit of trouble exporting a csv file that is created from one of my mysql tables using php.

The code I\'m using prints the correct data, but I can\'t see ho

5条回答
  •  轮回少年
    2021-01-27 06:41

    In essence, you can't output the CSV file and the link to it in one go. (You need to introduce the concept of a page "mode" and activate the download mode via a ...pagename.php?mode=download or similar. You could then use PHP's switch statement to switch on $_GET['mode'] in your script.)

    That said, the text/csv content type header you were using is correct, although you may also want to output the Content-Length and Content-Disposition headers. After you've output the file data, also be sure to stop any additional script processing via PHP's exit function.

    Additionally, it would probably be a lot less hassle (and will certainly be faster/more memory efficient) to use MySQL SELECT ... INTO OUTFILE facility (if you have the permissions) rather than use PHP to gather the data.

提交回复
热议问题