如何使用Zend Framework使用mySQL的INTO OUTFILE功能导出为CSV [英] How To Use Zend Framework To Export To CSV Using mySQL's INTO OUTFILE Functionality

查看:72
本文介绍了如何使用Zend Framework使用mySQL的INTO OUTFILE功能导出为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将大量数据导出到CSV文件中,以供用户使用Zend Framework下载.是否可以使用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 '\n' 
FROM test_table; 

推荐答案

您可以尝试构建基本查询,

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 Framework使用mySQL的INTO OUTFILE功能导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆