Hello everybody and thank you in advance for any suggestions and help I hope to receive.

I have Mysql experience but now I am working on my first M$sql database project, and I am getting frustrated. I am using php and would like to export a query. It is quite easy to export a query to csv with Mysql, but I can not figure out how or find suggestions to do so with M$sql.

To top it off, I am using Microsofts SQLSRV driver for php, from what I can tell uses a whole other query library than MySql and M$sql.

Does anybody have any experience exporting queries with MSSQL and PHP?

Again,

thanks in advance!

Hello Codewall -

Thank you for taking the time to help me out. I am sorry if I wasn't clear. What I would like to do is export a table in my MSSQL database to an csv formated file. I am not using MySQL.

Hello Codewall -

Thank you for taking the time to help me out. I am sorry if I wasn't clear. What I would like to do is export a table in my MSSQL database to an csv formated file. I am not using MySQL.

Hello,

Did you find the way to export data from MS SQL Server to csv with PHP ?

If yes, could you please tell me how I can do it ?

Thank you in advance for your help.

beegees

Here's the code dear.... edit according to you... where "edit here" is written in the code...

<?php
$con=dbconnect();
function dbconnect()  //Function to connect to the DB and to select the database from it.... EDit according to you
{
$db_server="localhost";
$db_db="";
$db_user="";
$db_password="";
$con = mysql_connect($db_server,$db_user,$db_password);
    if (!$con)
      {
            die('Could not connect: ' . mysql_error());
            }

    mysql_select_db($db_db, $con);
    return  $con;
}

function exportMysqlToCsv($table,$filename = 'example.csv')  //edit here...put your file name
{
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = "yoursqlquery";     //edit here
    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);
    $schema_insert = '';
    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for
    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;
    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {
                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed .
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= '';
            }
            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for
        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // 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");
    echo $out;
    exit;
}

$table="tablename"; // this is the tablename that you want to export to csv from mysql...... edit here
exportMysqlToCsv($table);
?>

oops sorry... i didn't saw it was MsSQL.... sorry.... but i'll try to find it for you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.