function save_query_result_to_csv($query, $filename, $headings = true) { $fp = fopen($filename, 'w'); $stmt = $this->execute($query, true); if ($stmt) { while ($row = oci_fetch_assoc($stmt)) { // Header row if (!isset($headings)) { $headings = array_keys($row); fputcsv($fp, $headings, ',', '"'); } // Put the row fputcsv($fp, csvsafe_row($row), ',', '"'); } } fclose($fp); }
function save_query_result_to_csv($query, $filename, $headings = true) { $fp = fopen($filename, 'w'); $stmt = $this->query($query); if ($stmt) { while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { // Header row if (!isset($headings)) { $headings = array_keys($row); fputcsv($fp, $headings, ',', '"'); } // Put the row fputcsv($fp, csvsafe_row($row), ',', '"'); } } fclose($fp); }
public function csv($headerrow = true) { $csvstring = ""; $tempfilename = ""; $temp = tmpfile(); if (!$temp) { $tempfilename = 'tmp' . rand(1000, 10000) . '.csv'; $temp = fopen($tempfilename, 'w'); } if (!$temp) { return false; } $res = $this->execute('row'); $count = 0; if (!empty($res)) { foreach ($res as $row) { if ($count == 0 && $headerrow) { $csvstring .= implode(",", array_keys($row)) . "\n"; } $count++; fputcsv($temp, csvsafe_row($row), ',', '"'); } $fileinfo = fstat($temp); fseek($temp, 0); $csvstring .= fread($temp, $fileinfo["size"]); } fclose($temp); if ($tempfilename != "") { unlink($tempfilename); } return $csvstring; }