使用php将Mssql表数据导出到csv文件中

时间:2013-01-28 22:52:00

标签: php sql-server csv export

当我运行php脚本导出MSSQL服务器表数据时,我收到此错误消息:

  

警告:mssql_query()[function.mssql-query]:消息:   无法发送仅Unicode归类或ntext数据中的Unicode数据   使用DB-Library(如ISQL)或ODBC 3.7或   早。 (严重级16)在C:\ Program Files \ Apache Software中   第22行的Foundation \ Apache2.2 \ htdocs \ Newitemtest \ csv3.php

有人能用正确的方式指出我吗?非常感谢你提前。

下面是我的php脚本

<?php
    $myServer = "****";
    $myUser = "**";
    $myPass = "****";
    $myDB = "***";

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

        $selected = mssql_select_db($myDB, $dbhandle)
            or die("Couldn't open database $myDB"); 

        $query = "SELECT * ";
        $query .= "FROM Item ";
        $result = mssql_query($query, $dbhandle);

    while ($l = mssql_fetch_array($result, MSSQL_ASSOC)) {
    foreach($l AS $key => $value){
        $pos = strpos($value, '"');
            if ($pos !== false) {
                $value = str_replace('"', '\"', $value);
            }
            $out .= '"'.$value.'",';
    }
    $out .= "\n";
    }
    mssql_free_result($result);
    mssql_close($dbhandle);

    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=export.csv");
    echo $out;
?>

0 个答案:

没有答案