Php 将SQL表导出为CSV,不显示列名

Php 将SQL表导出为CSV,不显示列名,php,html,mysql,excel,Php,Html,Mysql,Excel,下面是我目前用于将特定表导出为.csv格式的代码。到目前为止,它工作得非常好,因为它下载了文件,并按照我希望的方式格式化数据。但是,它不显示列名 例:它目前显示 1琼斯·马特 史密斯·约翰 简·多伊 <?php header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=applications.csv"); header("Pragma: no-cache"); he

下面是我目前用于将特定表导出为.csv格式的代码。到目前为止,它工作得非常好,因为它下载了文件,并按照我希望的方式格式化数据。但是,它不显示列名

例:它目前显示

1琼斯·马特

史密斯·约翰

简·多伊

<?php
header("Content-type: application/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", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$query = "SELECT * FROM applications";
$select_c = mysql_query($query) or die(mysql_error());

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;
?>
我希望它能显示:

ID姓氏姓氏

1琼斯·马特

史密斯·约翰

简·多伊

<?php
header("Content-type: application/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", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$query = "SELECT * FROM applications";
$select_c = mysql_query($query) or die(mysql_error());

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

以下是添加列名的快捷方法:

<?php
header("Content-type: application/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", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$query = "SELECT * FROM applications";
$select_c = mysql_query($query) or die(mysql_error());

// -------------------- add the line below -----------------------------------
$result="ID,LAST_NAME,FIRST_NAME,ORGANIZATION,TITLE\n";
// -------------------- add the line above -----------------------------------

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

只需在循环时在
之前添加标题行即可:

echo "ID,Last_Name,First_Name,ORGANIZATION,TITLE\n";

我假设您还想查询列名

使用SELECT语句,您将永远无法获得结果中的列名

在MySQL中,您可以使用

SHOW COLUMNS FROM applications FROM db;
获取结果中的列名


这是一种替代实现,允许您使用fputcsv来防止数据中出现逗号

<?php
header("Content-type: application/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", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$query = "SELECT ID, LAST_NAME, FIRST_NAME, ORGANIZATION, TITLE FROM applications";
$select_c = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($select_c, MYSQL_ASSOC);
$fp = fopen("php://output", "w");
fputcsv($fp, array_keys($row));
do {
    fputcsv($fp, $rows);
} while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC));

?>