使用PHP从MySQL DB更改CSV格式

时间:2013-07-04 15:51:16

标签: php mysql csv

我发现了一个类似的问题但是由于缺乏信息而没有解决方案。 我有下面的代码,它将连接的MySQL数据库中的数据输出为格式化的CSV文件,格式如下......

First Name:Surname:GCNumber:Dept:Start Date:Introduction:Theory:Fire Safety:Governance:
John:Smith:123456:HR:03/08/2013:Pass:Pass:Fail:Pass:
Jane:Watson:123445:IT:03/08/2013:Pass:Fail:Pass:Pass:
Mark:Byron:123442:IT:03/08/2013:Fail:Fail:Not Done:Not Done:

: =仅用于显示每列

它只是以相同的结构输出数据库中的所有行和列,并在导入到Excel时重命名列标题以使其更加友好。 我需要的是将此输出的格式更改为以下...

First Name:Surname:GCNumber:Dept:Email:Start Date:Module:Status:
John:Smith:123456:HR:03/08/2013:Introduction:Pass:
John:Smith:123456:HR:03/08/2013:Theory:Pass:
John:Smith:123456:HR:03/08/2013:Fire Safety:Fail:
John:Smith:123456:HR:03/08/2013:Governance:Pass:
Jane:Watson:123445:IT:03/08/2013:Introduction:Pass:
Jane:Watson:123445:IT:03/08/2013:Theory:Fail:
Jane:Watson:123445:IT:03/08/2013:Fire Safety:Pass:
Jane:Watson:123445:IT:03/08/2013:Governance:Pass:
Mark:Byron:123442:IT:03/08/2013:Introduction:Fail:
Mark:Byron:123442:IT:03/08/2013:Theory:Fail:
Mark:Byron:123442:IT:03/08/2013:Fire Safety:Not Done:
Mark:Byron:123442:IT:03/08/2013:Governance:Not Done:

: =仅用于显示每列

因此,不是每个人的一个条目,以及他们所完成的每个模块的结果,我需要将它作为每个模块的单独条目。 总共有这个数据库和35个模块有更多的字段,但为了便于说明,我已将其减少了。

作为PHP的新手,我正在努力解决如何做到这一点。 这是可能的,还是更容易尝试将DB的结构更改为所需的格式?

任何正确方向的帮助或指示都会很棒。 贝

<?php

function exportMysqlToCsv($table,$filename = 'db-snapshot.csv')
{

    $sql_query = "select fldFirstname as 'First Name',
     fldSurname as 'Surname',
     fldGMCNumber as 'GCNumber', 
     fldDestDept as 'Dept',
     fldStartDate as 'Start Date',
     fldModule1 as 'Introduction',
     fldModule2 as 'Theory',
     fldModule3 as 'Fire Safety',
     fldModule4 as 'Governance'
     from $table";

    // Gets the data from the database
    $result = mysql_query($sql_query);

    $f = fopen('php://temp', 'wt');
    $first = true;
    while ($row = mysql_fetch_assoc($result)) {
        if ($first) {
            fputcsv($f, array_keys($row));
            $first = false;
        }
        fputcsv($f, $row);
    } // end while

    $size = ftell($f);
    rewind($f);

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: $size");

 // Output to browser with appropriate mime type, you choose ;)
    header("Content-type: text/x-csv");
    // header("Content-type: text/csv");
    // header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    fpassthru($f);
    exit;
}

?>

2 个答案:

答案 0 :(得分:1)

您可以使用UNIONS:

SELECT fldFirstname as 'First Name', fldSurname as 'Surname', 
  fldGMCNumber as 'GCNumber', fldDestDept as 'Dept', 
  fldStartDate as 'Start Date', fldEndDate as 'End Date', 
  'Introduction' as 'Module', fldModule1 as 'Status' FROM records
UNION
SELECT fldFirstname,fldSurname,fldGMCNumber,fldDestDept,fldStartDate,fldEndDate,
  'Theory',fldModule2 FROM records
UNION
SELECT fldFirstname,fldSurname,fldGMCNumber,fldDestDept,fldStartDate,fldEndDate,
  'Fire Safety',fldModule3 FROM records
UNION
SELECT fldFirstname,fldSurname,fldGMCNumber,fldDestDept,fldStartDate,fldEndDate,
  'Governance',fldModule4 FROM records
ORDER BY Surname, `First Name`, Module;

SQL Fiddle example

答案 1 :(得分:0)

我会在查询数据库时使用一个数据透视表。检查下 If I have a MySQL table looking something like this

所以你基本上会创建一个包含描述你模块的行的表:

Module 
1 | Introduction 
2 | Theory 
3 | Fire Safety 
4 | Governance

然后将其加入上面的表并使用“Case”语法来实现您的请求。