【问题标题】:Exporting SQL table to CSV, column names not appearing将 SQL 表导出为 CSV,列名未出现
【发布时间】:2013-05-23 18:44:57
【问题描述】:

这是我目前用于将特定表导出为 .csv 格式的代码。到目前为止,它运行良好,因为它可以下载文件并按照我的意愿格式化数据。但是它不显示列名

ex: 目前显示

1 琼斯·马特

2 史密斯约翰

3 多伊简

我希望它显示:

ID 姓氏 名字

1 琼斯·马特

2 史密斯约翰

3 多伊简

<?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 html mysql excel


    【解决方案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());
    
    // -------------------- 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;
    ?>
    

    【讨论】:

      【解决方案2】:

      只需在 while 循环之前添加标题行:

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

      【讨论】:

        【解决方案3】:

        我假设您还想查询列名。

        使用 SELECT 语句,您永远不会在结果中获得列名。

        在 MySQL 中你可以使用

        SHOW COLUMNS FROM applications FROM db;
        

        获取结果中的列名。

        http://dev.mysql.com/doc/refman/5.7/en/show-columns.html

        【讨论】:

          【解决方案4】:

          另一种实现方式,允许您使用 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));
          
          ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-21
            • 2021-08-16
            • 2015-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多