简体   繁体   English

从Mysql导出CSV

[英]Export CSV from Mysql

I'm having a bit of trouble exporting a csv file that is created from one of my mysql tables using php. 我在导出使用php从我的mysql表之一创建的csv文件时遇到麻烦。

The code I'm using prints the correct data, but I can't see how to download this data in a csv file, providing a download link to the created file. 我使用的代码可以打印正确的数据,但是我看不到如何在csv文件中下载此数据,提供了指向已创建文件的下载链接。 I thought the browser was supposed to automatically provide the file for download, but it doesn't. 我以为浏览器应该会自动提供要下载的文件,但事实并非如此。 (Could it be because the below code is called using ajax?) (可能是因为以下代码是使用ajax调用的?)

Any help greatly appreciated - code below, S. 任何帮助,不胜感激-下面的代码,S。

include('../cofig/config.php');    //db connection settings
$query = "SELECT * FROM isregistered";
$export = mysql_query($query) or die("Sql error : " . mysql_error());
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "\t";
}
while ($row = mysql_fetch_row($export)) {
    $line = '';
    foreach ($row as $value) {
        if ((!isset($value) ) || ( $value == "" )) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line) . "\n";
}
$data = str_replace("\r", "", $data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";
}
//header("Content-type: application/octet-stream"); //have tried all of these at sometime
//header("Content-type: text/x-csv");
header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=export.csv");
//header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo '<a href="">Download Exported Data</a>'; //want my link to go in here...

print "$header\n$data";

In essence, you can't output the CSV file and the link to it in one go. 本质上,您无法一次性输出CSV文件及其链接。 (You need to introduce the concept of a page "mode" and activate the download mode via a ...pagename.php?mode=download or similar. You could then use PHP's switch statement to switch on $_GET['mode'] in your script.) (您需要引入页面“模式”的概念,并通过... pagename.php?mode = download或类似方法激活下载模式。然后,您可以使用PHP的switch语句打开$ _GET ['mode']在您的脚本中。)

That said, the text/csv content type header you were using is correct, although you may also want to output the Content-Length and Content-Disposition headers. 就是说,虽然您可能还希望输出Content-Length和Content-Disposition标头,但您使用的text / csv内容类型标头是正确的。 After you've output the file data, also be sure to stop any additional script processing via PHP's exit function. 输出文件数据后,还请确保通过PHP的exit功能停止任何其他脚本处理。

Additionally, it would probably be a lot less hassle (and will certainly be faster/more memory efficient) to use MySQL SELECT ... INTO OUTFILE facility (if you have the permissions) rather than use PHP to gather the data. 另外,使用MySQL SELECT ... INTO OUTFILE工具(如果您有权限)而不是使用PHP来收集数据,可能会少很多麻烦(并且肯定会更快/内存效率更高)。

You should not put the link in the same file that generates the csv, as the link will not be in the csv itself! 您不应将链接放在生成csv的同一文件中,因为链接不会在csv本身中!

Do something like: 做类似的事情:

<a href="fileThatGeneratesTheCSV.php">Download CSV</a>

and it should work 它应该工作

You can't have text and a download on the same page. 您不能在同一页面上显示文字和下载内容。 You need to have a link to the download area, which could just be a GET parameter leading to a function, which then does all the processing, displays headers, and echoes the content of the CSV. 您需要有一个指向下载区域的链接,该链接可能只是通向函数的GET参数,然后该函数执行所有处理,显示标题并回显CSV的内容。

For example, you could have <a href="foo.php?action=download">Click here to download CSV</a> , then in your code have if ($_GET['action'] === 'download') , get the data from the database, format it, send the headers, and echo the data. 例如,您可以<a href="foo.php?action=download">Click here to download CSV</a> ,然后在代码中包含if ($_GET['action'] === 'download') ,从数据库中获取数据,对其进行格式化,发送标头,然后回显数据。 And then die() , because that part of the script can accomplish no more. 然后die() ,因为脚本的那部分无法完成。

Three things to consider: 要考虑的三件事:

  1. You're sending headers indicating that the user is going to be downloading a CSV file, but then you send create a link to download it? 您正在发送标头,指示用户将要下载CSV文件,但是随后发送创建链接来下载该文件吗? This isn't correct, you should be linking to this page, and then only outputting the CSV data itself after the headers. 这是不正确的,您应该链接到此页面,然后仅在标题之后输出CSV数据本身。

  2. MySQL has the ability to generate CSV output, and you should definitely take advantage of this instead of trying to do it yourself. MySQL具有生成CSV输出的能力,您绝对应该利用此优势,而不是自己尝试这样做。 You can use SELECT INTO ... OUTFILE to do this. 您可以使用SELECT INTO ... OUTFILE来执行此操作。

  3. If you must create the CSV using PHP, please use fputcsv to do so. 如果必须使用PHP创建CSV,请使用fputcsv进行创建。 This will handle all the complications of CSV such as escaping and proper formatting. 这将处理CSV的所有复杂性,例如转义和正确格式化。 Since fputcsv writes to a file, you could either write it to a temporary file and then output it after you send your headers, or use the following trick to output it directly: 由于fputcsv写入文件,因此您可以将其写入临时文件,然后在发送头之后将其输出,或者使用以下技巧直接将其输出:

Do this after sending headers: 发送标头后执行以下操作:

$fp = fopen('php://output', 'w');
while( $row = mysql_fetch_row( $export ) ) {
    fputcsv($fp, $row);
}

I think the mySQL => CSV is common problem which is part of each PHP forum. 我认为mySQL => CSV是常见问题,它是每个PHP论坛的一部分。 I have try to solve this issue in a common way and implement an free export lib for PHP which is very similar to the Google AppInventor philosophie. 我试图以一种通用的方式解决此问题,并为PHP实现一个免费的导出库,这与Google AppInventor的理念非常相似。 DragDrop and hide the coding stuff. DragDrop并隐藏编码内容。

Use the lib and create your Export via Click&Point. 使用库并通过Click&Point创建导出。

Common Demos: http://www.freegroup.de/software/phpBlocks/demo.html Link to editor: http://www.freegroup.de/test/editor/editor.php?xml=demo_sql.xml 常见演示: http : //www.freegroup.de/software/phpBlocks/demo.html链接到编辑器: http ://www.freegroup.de/test/editor/editor.php?xml = demo_sql.xml

worth a look 值得一看

Greetings 问候

Andreas 安德烈亚斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM