我看到这个帖子了,但它并没有真正提供一个解决方案(已经至少对我来说工作)...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