In CodeIgniter 2, I'm generating a CSV file from fetching data from my Oracle Database (volume of data can be a less than 10 lines to hundreds of thousands of lines, this is why I "unset" each $line) using the following code :
$conn = $this->db;
$stid = oci_parse($conn->conn_id, $sql);
oci_execute($stid);
// Header extraction
$headers = "";
$values = "";
$row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS );
foreach($row as $key => $value) {
$headers = $headers.$key.';';
$values = $values.$value.';';
}
// Remove of last comma and adding in a line break
$headers = substr($headers,0,-1). "\r\n";
$values = substr($values,0,-1). "\r\n";
$data = $headers . $values;
$values = "";
// Parsing all data to concatenate the values
while (($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) != false) {
$line = "";
foreach ($row as $key => $value) {
$line .= $value . ';';
}
$line = substr($line, 0, -1) . "\n";
$data = $data . $line;
// Freeing memory for the line
unset($line);
}
return $data;
Using this piece code, my CSV file is properly generated except for one thing.
When I'm exporting just a small number of lines, everything is fine. I have all the values, for ALL lines, comma seperated. However, when I'm exporting a few hundred or thousand of lines, the LAST value from the LAST line is always truncated by 5 characters (data in the database is OK).
HEADER1;HEADER2;HEADER3;HEADER4;HEADER5
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
[.......]
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX
I thought maybe it was some cache limitation or something, but it happens after a few hundred lines exported and wether it's just a few hundred or several hundred thousand lines.
I can't figure this one out ...
Can anybody help ?
Thanks in advance.
fputcsv()function rather than outputting it yourself with that foreach loop.fputcsv()will output to a file. However, if you're struggling with memory space (which I can see you would be doing the above), consider exporting the CSV to a temp file on disk; then all PHP has to do isreadfile()to send it to the browser and then delete it. If you can get Oracle to output the CSV file for itself, then PHP's memory limits never need to be worried about. And it'll be a lot quicker.