I'm trying to write the results of a MYSQL query to a CSV and having issues. The query works, and when I run in powershell it prints the perfect CSV data in powershell, but the file is nowhere to be found on my CPU.
How can I physically write to a CSV?
$result = $conn2->query(
"SELECT firstn
, lastn
, extension
, Recieved
, RecievedKnown
, Outbound
, outboundKnown
, Missed
, MissedKnown
, CallingNumber
, CalledNumber
, starttime
, endtime
, duration
, HOLDTIMESECS
, TERMINATIONREASONCODE
FROM (
SELECT u.firstn
, u.lastn")
//Long query, removing unnecessary code
);
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
$headers[] = $fieldinfo->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="dailyReportTestPHP.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
$fp = fopen('php://output', 'w');mysql_num_fields