How To Use Zend Framework To Export To CSV Using mySQL's INTO OUTFILE Functionality

前端 未结 2 1331
一生所求
一生所求 2021-01-22 00:59

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\

2条回答
  •  清歌不尽
    2021-01-22 01:36

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

提交回复
热议问题