I have the following code that exports the results from an sql query out to a comma separated file. The problem is that it is slow and hangs up sometimes.
Does anyone have a more efficient code?
<?php
/ This actually creates the csv file. (ejc 10/22/02) /
require_once("deliveryfmt_functions.php"); // Load the library
deliveryfmt_initialize(); // Initialize the library
deliveryfmt_set_format('csv', false); // Select EXCEL (.csv) formatting
/ header('Content-Type: '.deliveryfmt_mimetype()); / // MIME-type
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment;filename="results.'.deliveryfmt_format_extension().'"'); // Try to name the output file
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); # Date in the past
header('Last-Modified: '.gmdate("D, d M Y H:i:s").' GMT'); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
/$nocache = 'Cache-Control: no-cache'; / // HTTP/1.1
//header($nocache);
header('Content-Transfer-Encoding: binary'); # Binary data
$conn = pg_pconnect("user=jjkkll password=mno2025 dbname=tester");
if (!$conn){
echo "An error occured\n";
exit; }
/* execute the SQL statement */
$strSQL2 = $_POST['strSQL'];
$result_set=pg_Exec($conn,$strSQL2);
$rows = pg_NumRows($result_set);
/* Put out the column headings */
$fields=pg_numfields($result_set);
for($i=0;$i<$fields;$i++){
if ($i == '0') {
/**********************CHANGE FIRST ELEMENT TO AN ARRAY ELEMENT FOR PHP 5 ****************************/
$arr_field = (array(pg_fieldname($result_set,$i)));
} else {
$arr_field=array_merge($arr_field,array(pg_fieldname($result_set,$i)));
}
}
deliveryfmt_set_column_names($arr_field);
deliveryfmt_header(); # Set up file header
/* Put out the data */
for($i=0;$i<$rows;$i++){
$myrow = pg_fetch_row($result_set,$i);
deliveryfmt_put_array($myrow);
}
deliveryfmt_trailer(); // Add file trailer
$buffer = deliveryfmt_finish(); // Finish up the Excel file and return the contents
echo $buffer; // Send it to the waiting browser
$arr_field="";
pg_close();
?>