简体   繁体   English

如何使用PHP / Zend Framework1以流形式获取数据库结果

[英]How to fetch database result as a stream using PHP/Zend Framework1

I'm writing a CSV file with the results from a function that returns database query results as an array, and getting memory out of exception if the result set is large. 我正在写一个CSV文件,其中包含来自函数的结果,该函数将数据库查询结果作为数组返回,并且如果结果集很大,则将内存移出异常。

So I'm looking for writing CSV using streams, can anyone help me how to do this using PHP or Zend Framework-1. 因此,我正在寻找使用流编写CSV的方法,有人可以帮助我如何使用PHP或Zend Framework-1做到这一点。

You have to change your function to return a statement , not full results. 您必须更改函数以返回语句 ,而不是完整结果。 You can use fetch method to get database results per-row and fputcsv to write into a csv file. 您可以使用fetch方法按行获取数据库结果,并使用fputcsv写入csv文件。

$fp = fopen('file.csv', 'w');
$stmt = $db->query('SELECT * FROM table WHERE smth IS NOT NULL ORDER BY id DESC');

while ($row = $stmt->fetch()) {
    fputcsv($fp, $fields);
}

fclose($fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM