I just want to double check the most efficient method of writing to a CSV file with PHP using data retrieved from a database.
Currently after executing my sql query to retrieve the data I am using a while loop to assign the data to varaibles, then writing to the file within that while loop. Like so
$fp = fopen("../" . $this->portal . "/" . $this->folder . "/" . $this->fileName . ".csv", 'a+');
$data = array();
while ($row = sqlsrv_fetch_array($getList, SQLSRV_FETCH_ASSOC)) {
$data['id'] = $row['id'];
$data['empId'] = $row['empId'];
$data['fullname'] = $row['fullname'];
$data['title'] = $row['title'];
$data['department'] = $row['department'];
fputcsv($fp, array_values($data));
}
fclose($fp);
I'm wondering if it would be quicker to assign the data from each iteration to a string variable in csv format, then concatenate that throughout the loop. Then once the loop is completed, write that variable to a file? Would that be quicker in any way? Or is there another way entirely to write to a csv file which is more efficient?