How To Use Zend Framework To Export To CSV Using mySQL#39;s INTO OUTFILE Functionality(如何使用 Zend 框架使用 mySQL 的 INTO OUTFILE 功能导出到 CSV)
问题描述
我希望使用 Zend Framework 将大量数据导出到 CSV 文件中以供用户下载.有什么方法可以使用 Zend_Db 的功能并使用INTO OUTFILE"语法将文件输出为 csv?基本上我希望能够调整我的 Zend_Db 模型以导出到一个 csv 文件而不是返回一个 PHP 数组.我想使用的 mysql 语法的一个例子是:
I am looking to export a large amount of data into a CSV file for user download using Zend Framework. Is there any way to use Zend_Db's functionaity and use the "INTO OUTFILE" syntax to output the file as a csv? Basically I want to be able to adapt my Zend_Db models to export to a csv file instead of returning a PHP array. An example of the mysql syntax I want to use would be:
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '
'
FROM test_table;
推荐答案
您可以尝试构建一个基本查询,将其转换为字符串,然后从那里追加.优点是您可以使用 Zend 的绑定功能(至少对于 where 子句),以防万一.像这样:
You can try to build a base query, convert it to a string and then append from there. The advantage being that you can use Zend's bind functionality (at least for the where clause), in case you need that. Like so:
$filename = 'tmp/test.csv';
$value = 'given';
// build base query making use of Zend´s binding feature.
$select = $this->_db->select()
->from(test_table, array(a,b,a+b))
->where('column = ?', $value)
->__toString()
// append the rest of the query.
.' INTO OUTFILE "'. $filename .'"'
.' FIELDS TERMINATED BY ";"'
.' OPTIONALLY ENCLOSED BY "\""'
.' LINES TERMINATED BY "\n"';
// execute query.
$this->_db->query($select);
这篇关于如何使用 Zend 框架使用 mySQL 的 INTO OUTFILE 功能导出到 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Zend 框架使用 mySQL 的 INTO OUTFILE 功能导出到 CSV


基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01