-1

I am trying to:

  1. create comma delimited .csv file with php code.
  2. Insert column name in the first line of the .csv file.

Please advise how can I make this happen.

    <?php

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=".$batch_id.".csv");
    header("Pragma: no-cache");
    header("Expires: 0");   

    $conn_sp = mssql_connect("SQLServer", "user", "password"); 
    $db_sp = mssql_select_db("databaseName", $conn_sp); 
    $stmt = mssql_init("[StoredProcedureName]",$conn_sp);       
    mssql_bind($stmt, "@BatchNo", $batch_id, SQLVARCHAR, FALSE, FALSE, 20);     
    $result = mssql_query("SET ANSI_NULLS ON"); 
    $result = mssql_query("SET ANSI_WARNINGS ON");      
    $result = mssql_execute($stmt);
    $record="";
    $comma=",";
    $record_end="\n";
    while ($row = mssql_fetch_array($result, MSSQL_ASSOC)){ 

    foreach ($row as $key => $value) {      

          echo $value;      
        }
        echo "\n";  

    }?>

! enter image description here

Paul
  • 47
  • 1
  • 9
  • See http://php.net/manual/en/function.fputcsv.php. – chris85 Sep 30 '16 at 15:35
  • 2
    Possible duplicate of [Creating csv file with php](http://stackoverflow.com/questions/15501463/creating-csv-file-with-php) – chris85 Sep 30 '16 at 15:35
  • I tried these articles but didn't work. Not sure but I am doing wrong, so please if you can advise on the above code, I will appreciate. Thank you! – Paul Sep 30 '16 at 15:38
  • First thing first, if you remove the headers, then you got something right?...can you post the output you are getting? – Hackerman Sep 30 '16 at 15:41
  • Please see the output of the code – Paul Sep 30 '16 at 15:54
  • Show how the `fputcsv` failed for you. It should be clear why you don't have a CSV currently (Hint: defining a variable doesn't cause it to do anything `$comma`). – chris85 Sep 30 '16 at 17:08

1 Answers1

0

Well just look at how a .csv-file is created.

Usually like this:

Delimiter: ,

New line: \n

Enclose entry by: "

e.g.

"r1a","r1b","r1c"

"r2a","r2b","r2c"

etc...

This is how your code should work (untested):

         echo '"colname1","colname2",...';

         while ($row = mssql_fetch_array($result, MSSQL_ASSOC)){ 
        
            $first = true;
            foreach ($row as $key => $value) {      
    
                if(!$first) {
                     echo ",";
                }
        
                  echo '"'.$value.'"';      
                }
                echo "\n";  
                $first=false;
            }
Community
  • 1
  • 1
Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Don't make your own CSV parser. What if `$value` has double quotes, commas? – chris85 Sep 30 '16 at 17:07
  • So, what's the best way to go, Chris? Can u please share example? Thank you! – Paul Sep 30 '16 at 17:12
  • @Paul Please use the `@` to tag users, notifications aren't sent otherwise. You should use the built in option, http://php.net/manual/en/function.fputcsv.php. There are examples on that page. If you are having issues with that post your usage and what happens. – chris85 Sep 30 '16 at 17:40
  • @chris85 In most cases this own .csv parser will do perfectly well. The only issue I can think of is with double quotes - it can be solved like here: http://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv – Blackbam Sep 30 '16 at 19:11