我看到這個帖子了,但它並沒有真正提供一個解決方案(已經至少對我來說工作)...PHP MySQL的導出爲CSV - 結果顯示HTML
我有一個PHP頁面它執行一些基本的MySQL查詢,其結果顯示在頁面上。我在整個過程中使用了一些$ _GET和$ _SESSION變量。
在同一頁面中,我還允許用戶「導出到CSV」(通過調用函數)。從導出返回的文件底部有CSV結果,但也包含我的頁面的HTML(它調用函數)。
現在,在頁面頂部,我有ob_start()和底部我有ob_flush(),否則在頁面加載時,我將收到一些「無法修改標題...」錯誤。所以,正如在帖子中建議,我讀了:
My guess is that you've got some sort of template that generates the same HTML header and footer regardless of the page that is requested. Sometime before the exportCSV function is called, the header is generated.
You don't show the bottom of the output, but I'll bet the footer is there too, since I suspect the flow control will continue on to that code after the function exits."
(http://stackoverflow.com/questions/207019/why-am-i-getting-html-in-my-mysql-export-to-csv/207046)
有沒有人有任何想法,我如何能夠防止這種情況發生?讓我知道如果我應該發佈我的一些代碼,我會...
謝謝!
編輯:
當調用ob_end_clean()之前,我打電話給我的導出功能,它擺脫了CSV之前的任何HTML的。不過,我仍然得到一些HTML結束標記的CSV結果之後......
fname lname MONTH WeekdayCount WeekendCount
John Doe 8 1 0
John Doe 7 3 2
Jane Smith 7 3 2
</div>
</div>
</div>
</body>
</html>
編輯:
這個問題已經通過調用調用CSV導出腳本後退出()來解決。
感謝您的建議,請參閱我的編輯.. – littleK 2009-07-29 00:30:05