I have this PHP script to update a specific table of MySQL db:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
//Include connection file
require_once('../connection/connect.php');
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.csv");
header("Pragma: no-cache");
header("Expires: 0");
$sql = "SELECT * FROM patient";
$stmt = $conn->prepare($sql);
$stmt->execute();
//var_dump($stmt->fetch(PDO::FETCH_ASSOC));
$data = fopen('backup.csv', 'w');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
// Export every row to a file
fputcsv($data, $row);
}
?>
I created a file in the same directory called: backup.csv
and the data are updated correctly each time I execute this script. But the problem is that I can't add a header to table inside the .csv
file.
And the downloaded file is empty. I need to download backup.csv
and not reports.csv
.
I tried:
header("Content-Disposition: attachment; filename="'.$data.'";");
But it didn't helped. The file is still empty.