sql - Export large data to CSV using PHP -
i trying export data csv using php.
my code:
function download_send_headers($filename) { // disable caching $now = gmdate("d, d m y h:i:s"); header("expires: tue, 03 jul 2001 06:00:00 gmt"); header("cache-control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); header("last-modified: {$now} gmt"); // force download header("content-type: application/force-download"); header("content-type: application/octet-stream"); header("content-type: application/download"); // disposition / encoding on response body header("content-disposition: attachment;filename={$filename}"); header("content-transfer-encoding: binary"); } $giventerm = $_post['textboxlist']; $sql_query = "select * some_table key=".$giventerm; $result = sqlsrv_query($conn,$sql_query); $r1 = array(); $file = fopen("php://output", 'w'); while ($row = sqlsrv_fetch_array($result, sqlsrv_fetch_numeric)){ $valuesarray=array(); foreach($row $name => $value) { $valuesarray[]=$value; } fputcsv($file,$valuesarray); } fclose($file); download_send_headers("data_export_" . date("y-m-d") . ".csv"); die();
here receiving search-value form in page , triggering query based on it. code working fine smaller data-sets(up 100). when number of rows more in result (around 600-700) code crashes. please tell me how fix this.
p.s. using sql server 2012 database.
instead of first getting results in single array , outputting it, fetch row row , outputting each row directly. way won't need massive amount of memory fit entire array in.
Comments
Post a Comment