Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}
}

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=BOP.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('ID','Common Name', 'Scientific Name', 'Breeds', 'Abundance', 'Occurrence', 'Seasonal Status','AOU_Order '));

// fetch the data
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');

// loop over the rows, outputting them
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);
?>
Posted
Updated 16-Aug-17 23:18pm
v2

First of all - MySQL can export data to CSV directly from SELECT query: MySQL :: MySQL 5.7 Reference Manual :: 13.2.9 SELECT Syntax[^] (look for INTO OUTFILE)...

If you have to have use PHP you can use fputcsv[^] inside your loop over the rows... (sample 18 on that page)
 
Share this answer
 
Triggering the "Save As" dialog box belongs to the user himself. And chrome like application has habit of deciding by itself. The simplest way to do this is setting the header. Here is an example available in stackoverflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900