将内容从 SQL 数据库表导出到 .csv 文件。如果我添加列名,则在尝试打开下载的 .csv 文件时会出现以下错误:
但是,如果我在没有列的情况下导出它,它会很好地打开而不会出现错误。我尝试将 Content-Type: 从 text/csv 更改为 application/csv,但没有更改任何内容。我没主意了。
<?php
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=applications.csv");
header("Pragma: no-cache");
header("Expires: 0");
ini_set('display_errors',1);
$private=1;
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("cif") or die(mysql_error());
$query = "SELECT * FROM applications";
$select_c = mysql_query($query) or die(mysql_error());
/* The line below is what I comment out to remove the column names */
$result.="ID,LAST_NAME,FIRST_NAME,ORGANIZATION,TITLE\n";
/* The line above is what I comment out to remove the column names */
while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC))
{
$result.="{$row['ID']},";
$result.="{$row['LAST_NAME']},";
$result.="{$row['FIRST_NAME']},";
$result.="{$row['ORGANIZATION']},";
$result.="{$row['TITLE']},";
$result.="\n";
}
echo $result;
?>