Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:13:07+00:00 2026-05-28T05:13:07+00:00

I am exporting a SQL Server table to a CSV using php (see code

  • 0

I am exporting a SQL Server table to a CSV using php (see code below). What I am needing help with is including the headers of the columns in the export. I can do this when I export from MySQL, but cannot figure it out with SQL Server. If anyone can point me in the right direction I would appreciate it.

<?php

// SQL Server connection string details.
    $myServer = "server";
    $myUser = "user";
    $myPass = "password";
    $myDB = "dbname";

// connection to the SQL Server
    $dbhandle = mssql_connect($myServer, $myUser, $myPass)
            or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with
        $selected = mssql_select_db($myDB, $dbhandle)
            or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database
        $query = "SELECT col01, col02, col03, col04, col05, col06, col07 ";
        $query .= "FROM table ";
        $result = mssql_query($query, $dbhandle);

//Generate CSV file - Set as MSSQL_ASSOC as you don't need the numeric values.
    while ($l = mssql_fetch_array($result, MSSQL_ASSOC)) {
    foreach($l AS $key => $value){
        //If the character " exists, then escape it, otherwise the csv file will be invalid.
            $pos = strpos($value, '"');
            if ($pos !== false) {
                $value = str_replace('"', '\"', $value);
            }
            $out .= '"'.$value.'",';
    }
    $out .= "\n";
    }

//free result set memory
    mssql_free_result($result);

//close the connection
    mssql_close($dbhandle);

// Output to browser with the CSV mime type
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=export.csv");
    echo $out;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:13:08+00:00Added an answer on May 28, 2026 at 5:13 am

    Use http://php.net/manual/en/function.mssql-fetch-field.php , like this:
    http://www.razorsql.com/articles/sqlserver_column_names_values.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm exporting data programatically from Excel to SQL Server 2005 using SqlBulkCopy. It works
I am programmatically exporting data (using PHP 5.2) into a .csv test file. Example
i;m trying to automate the exporting of data from an sql server table to
I have a table in SQL Server which looks like this: ID Code Name
What are best practices for moving/exporting SQL Server Integration Services Packages from a SQL
In my C# windows application, I am exporting sql server data to excel on
Exporting some data from mysql to a csv file using FasterCSV. I'd like the
I am trying to bulk export out of a sql table, the code works
Here's a table with a computed column in SQL Server 2005: CREATE TABLE footable
On SQL Server 2008 R2, I am trying to read XML value as table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.