使用PHP将SQL下载到CSV [英] Download SQL to CSV using PHP
本文介绍了使用PHP将SQL下载到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将SQL查询下载到CSV文件。
以下代码有效,但不会触发保存或打开对话框。
以下文件来自链接
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);
?>
推荐答案
theValue,
theType,
theDefinedValue =,
theDefinedValue = "",
这篇关于使用PHP将SQL下载到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文