简体   繁体   English

将MySQL表导出到CSV文件不起作用

[英]Export mysql table to CSV file not working

I got the output printed in the browser,while i need to download it as CSV file. 我将输出打印在浏览器中,同时我需要将其下载为CSV文件。 Below is my code 下面是我的代码

$output        = "";
$table         = ""; // Enter Your Table Name 
$sql           = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);

while ($row = mysql_fetch_array($sql)) {
  for ($i = 1; $i < $columns_total-1; $i++) {
    $output .='"'.$row["$i"].'",';
  }
  $output .="\n";
}

// Download the file //下载文件

$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;

Why are you generating the CSV file manually? 为什么要手动生成CSV文件? There is a built in function for that; 有一个内置的功能; fputcsv . fputcsv

<?php

// generate csv
$table = 'my_table';
$output_file = 'file.csv';
$delimiter = ',';
$enclosure = '"';
$h = fopen($output_file, 'w');
$sql = mysql_query("select * from $table");
while ($row = mysql_fetch_assoc($sql)) {
    fputcsv($h, $row, $delimiter, $enclosure);
}
fclose($h);

// force download csv
header("Content-type: application/force-download"); 
header('Content-Disposition: inline; filename="'. $output_file .'"'); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-length: ". filesize($output_file)); 
header('Content-Type: application/excel'); 
header('Content-Disposition: attachment; filename="'. $output_file .'"');

?>

ps If you require the table column in the csv as headers, you can do this instead when generating the csv file: ps如果需要csv中的表列作为标题,则可以在生成csv文件时执行以下操作:

// generate csv
$table = 'my_table';
$output_file = 'file.csv';
$delimiter = ',';
$enclosure = '"';
$h = fopen($output_file, 'w');
$sql = mysql_query("select * from $table");
$header_written = false;
while ($row = mysql_fetch_assoc($sql))
{
    if (!$header_written) {
        fputcsv($h, array_keys($row), $delimiter, $enclosure);    
        $header_written = true;
    }
    fputcsv($h, $row, $delimiter, $enclosure);
}
fclose($h);

Use this function 使用这个功能

    function force_download($file)
    {
        $dir = ""; // directory path if any
        if ((isset($file))&&(file_exists($dir.$file))) 
        {
           header("Content-type: application/force-download");
           header('Content-Disposition: inline; filename="' . $dir.$file . '"');
           header("Content-Transfer-Encoding: Binary");
           header("Content-length: ".filesize($dir.$file));
           header('Content-Type: application/octet-stream');
           header('Content-Disposition: attachment; filename="' . $file . '"');
           readfile("$dir$file");
        }
    }//end function

    $filename = "myFile.csv";
    force_download($filename);

I had this problem as well, no matter what combination of "header" I used. 无论我使用哪种“标题”组合,我也遇到这个问题。

The solution for me was because I had some HTML in my "download.php" file. 对我来说,解决方案是因为我的“ download.php”文件中包含一些HTML。 I guess if the browser detects any HTML, it will display the data in the browser instead of initializing the download. 我想如果浏览器检测到任何HTML,它将在浏览器中显示数据,而不是初始化下载。

Delete any HTML in your "download.php" file and leave only PHP--- it should work. 删除“ download.php”文件中的所有HTML,仅保留PHP ---应该可以。

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

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