把一个sql查询结果,输出到浏览器或者excel下载
发布日期:2021-09-20 08:07:15
浏览次数:46
分类:技术文章
本文共 938 字,大约阅读时间需要 3 分钟。
<?php function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true) { if($attachment) { // send response headers to the browser header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment;filename='.$filename); $fp = fopen('php://output', 'w'); } else { $fp = fopen($filename, 'w'); } $result = mysql_query($query, $db_conn) or die( mysql_error( $db_conn ) ); if($headers) { // output header row (if at least one row exists) $row = mysql_fetch_assoc($result); if($row) { fputcsv($fp, array_keys($row)); // reset pointer back to beginning mysql_data_seek($result, 0); } } while($row = mysql_fetch_assoc($result)) { fputcsv($fp, $row); } fclose($fp); } // Using the function $sql = "SELECT * FROM table"; // $db_conn should be a valid db handle // output as an attachment query_to_csv($db_conn, $sql, "test.csv", true); // output to file system query_to_csv($db_conn, $sql, "test.csv", false); ?>
转载地址:https://blog.csdn.net/caiyiii/article/details/10449799 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年03月26日 04时14分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
hdfs linux 目录是否存在,Linux中判断hdfs文件是否存在
2019-04-21
linux学习需要什么基础,学linux需要什么基础?
2019-04-21
高斯勒让德在c语言中的程序,c语言:用递归方法编写程序,求n阶勒让德多项式的值...
2019-04-21
c语言单片机电子时钟,新人求个51单片机的电子时钟汇编语言(C语言的还没学到)...
2019-04-21
c++语言文件流,C++文件流
2019-04-21
android 动态毛玻璃,Android毛玻璃背景效果简单实现代码
2019-04-21
android 按钮提示,的Android按钮工具提示
2019-04-21
win7自定义html为桌面,Win7系统自定义桌面主题的方法
2019-04-21
linux存储pdf伟岸_python的reportlab库介绍、制作pdf和作图
2019-04-21
安徽信息技术初中会考上机考试模拟_2020年中小学寒假、考试时间定下了!
2019-04-21
稳定币usda是哪个发行的_武夷山币装帧款曝光,共4款设计,你喜欢哪款?
2019-04-21
苹果笔记本的end键_笔记本用户的大烦恼:触控板,想好好用你不容易
2019-04-21
趣玩机器人什么时候成立的_【直播回顾】当我们谈机器人集成调试的时候在谈什么...
2019-04-21