-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

6
  • See php.net/manual/en/function.fputcsv.php. Commented Sep 30, 2016 at 15:35
  • 2
    Possible duplicate of Creating csv file with php Commented Sep 30, 2016 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! Commented Sep 30, 2016 at 15:38
  • First thing first, if you remove the headers, then you got something right?...can you post the output you are getting? Commented Sep 30, 2016 at 15:41
  • Please see the output of the code Commented Sep 30, 2016 at 15:54

1 Answer 1

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;
            }
Sign up to request clarification or add additional context in comments.

4 Comments

Don't make your own CSV parser. What if $value has double quotes, commas?
So, what's the best way to go, Chris? Can u please share example? Thank you!
@Paul Please use the @ to tag users, notifications aren't sent otherwise. You should use the built in option, 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 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: stackoverflow.com/questions/17808511/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.