I am trying to download a SQL query to a CSV file.
The code below works, but does not trigger tyhe "Save or Open" dialogue box.
The file below is run from a link
<p><a href="TestDownload.php">Download</a></p>
How do I trigger the dialogue box?
What I have tried:
<?php require_once('../Connections/PaSiteGuide.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=BOP.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('ID','Common Name', 'Scientific Name', 'Breeds', 'Abundance', 'Occurrence', 'Seasonal Status','AOU_Order '));
mysql_select_db($database_PaSiteGuide, $PaSiteGuide);
$rows = mysql_query('SELECT BirdsOfPa.ID, BirdsOfPa.CommonName, BirdsOfPa.SciName, BirdsOfPa.Breeding, BirdsOfPa.Abundance, BirdsOfPa.Occurrence, BirdsOfPa.SeasonalStatus, AOU_List.AOU_Order FROM BirdsOfPa, AOU_List WHERE BirdsOfPa.ID = AOU_List.SPNO ORDER BY AOU_List.AOU_Order ASC');
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
?>
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
<?php
mysql_free_result($rsTestBOP);
?>