How to use Zend Framework to export to CSV Using mySQL INTO OUTFILE Functionality

I am looking to export a large amount of data to a CSV file to load a user using the Zend Framework. Is there a way to use the Zend_Db functionality and use the "INTO OUTFILE" syntax to output the file as csv? Basically I want to be able to adapt my Zend_Db models for export to a csv file instead of returning a PHP array. An example of the mysql syntax I want to use is:

SELECT a,b,a+b INTO OUTFILE '/tmp/result.text' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
FROM test_table; 
+2
source share
2 answers

Using Zend_Db $ db, you can directly send any request:

$db->query($exportquery);

, , , FILE, (only root MySQL ): ). , MySQL, , OUTFILE.

0

, , . , Zend bind ( , where), . :

$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);
0

All Articles