Php 将mysql表导出到CSV文件无效

Php 将mysql表导出到CSV文件无效,php,Php,我得到了在浏览器中打印的输出,而我需要下载它作为CSV文件。 下面是我的代码 $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

我得到了在浏览器中打印的输出,而我需要下载它作为CSV文件。 下面是我的代码

$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";
}

使用此功能

    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);

为什么要手动生成CSV文件?这有一个内置函数


我也有这个问题,无论我使用什么组合的“头”

我的解决方案是因为我的“download.php”文件中有一些HTML。 我猜如果浏览器检测到任何HTML,它将在浏览器中显示数据,而不是初始化下载


删除“download.php”文件中的任何HTML,只保留php——它应该可以工作。

p.S.
mysql.*
不推荐使用。您应该查看
MySQLi
PDO
。仍在浏览器中打印结果而不强制下载我的代码仅显示如何从db结果生成csv文件,并强制输出为下载到浏览器。里面没有任何连接代码。你记得在那里添加数据库连接代码吗?i、 e.
mysql\u connect
mysql\u close
    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);
<?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 .'"');

?>
// 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);