php PHP中的MSSQL查询问题和查询文本数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5414890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:19:41  来源:igfitidea点击:

MSSQL Query issue in PHP and querying text data

php

提问by megazoid

I'm trying a query in PHP to connect and extract data from a MSSQL EXPRESS (2008 R2) database. But i'm getting an error when i'm pulling ntext based data from the DB.

我正在尝试使用 PHP 进行查询以连接并从 MSSQL EXPRESS (2008 R2) 数据库中提取数据。但是当我从数据库中提取基于 ntext 的数据时出现错误。

Error is;

错误是;

Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. (severity 16) in

不能使用 DB-Library(例如 ISQL)或 ODBC 3.7 或更早版本将 Unicode 归类中的 Unicode 数据或 ntext 数据发送到客户端。(严重性 16)在

and my script is

我的脚本是

    $myServer = ".\SQLEXPRESS";
    $myUser = "sa";
    $myPass = "blablabla";
    $myDB = "test"; 

    //connection to the database
    $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 * FROM dbo.table WHERE query='2'";
    //$query .= "FROM dbo.table  ";
    //$query .= "WHERE query='2'"; 

    //execute the SQL query and return records
    $result = mssql_query($query);

    $numRows = mssql_num_rows($result); 
    echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

    //display the results 
    while($row = mssql_fetch_array($result))
    {
      echo "<li>" . $row["query"]. "</li>";
    }
    //close the connection
    mssql_close($dbhandle); 

any help on this is appreciated ....

对此的任何帮助表示赞赏....

Thanks ....

谢谢 ....

回答by Phil

Couple of options from the comments on the mssql_query()manual page

手册页评论中的几个选项mssql_query()

  • SELECT CAST(field1 AS TEXT) AS field1 FROM table
  • Chang the version in /etc/freetds.conffrom 4.2 to 8.0 (if the PHP server is *nix)
  • Avoid SELECT *queries
  • SELECT CAST(field1 AS TEXT) AS field1 FROM table
  • 将版本/etc/freetds.conf从 4.2 更改为 8.0(如果 PHP 服务器是 *nix)
  • 避免SELECT *查询

Plenty more if you search ntexton that page.

如果您ntext在该页面上搜索,还有更多。

回答by Yura Rodchyn

Here are some things you might need to know:

以下是您可能需要了解的一些事项:

  1. Install mssql support for Debian (Lenny/Squeeze):

    apt-get install php5-sybase

  2. When you got this error message: "Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier."

    In /etc/freetds/freetds.conf add these two lines (last two):

    [global]
    ;tds version = 4.2
    tds version = 8.0
    client charset = UTF-8
    

    You can edit "charset" in php.ini too (but you don't need if you did it previously in freetds.conf): ; Specify client character set.. ; If empty or not set the client charset from freetds.comf is used ; This is only used when compiled with FreeTDS

    mssql.charset = "UTF-8"
    
  3. Use nchar/nvarchar/ntext column types if you need unicode support.

  1. 为 Debian 安装 mssql 支持(Lenny/Squeeze):

    apt-get 安装 php5-sybase

  2. 当您收到以下错误消息时:“无法将仅 Unicode 归类中的 Unicode 数据或 ntext 数据发送到使用 DB-Library(例如 ISQL)或 ODBC 3.7 版或更早版本的客户端。”

    在 /etc/freetds/freetds.conf 添加这两行(最后两行):

    [global]
    ;tds version = 4.2
    tds version = 8.0
    client charset = UTF-8
    

    您也可以在 php.ini 中编辑“字符集”(但如果您之前在 freetds.conf 中做过,则不需要):; 指定客户端字符集.. ; 如果为空或未设置,则使用来自 freetds.comf 的客户端字符集;这仅在使用 FreeTDS 编译时使用

    mssql.charset = "UTF-8"
    
  3. 如果需要 unicode 支持,请使用 nchar/nvarchar/ntext 列类型。

回答by Oscar Intecfra

In my case, I needed to install:

就我而言,我需要安装:

sudo apt-get install php-sybase

And modify the /etc/freetds.conf file:

并修改 /etc/freetds.conf 文件:

...
[global]
    # TDS protocol version
;   tds version = 4.2
tds version = 8.0
client charset = UTF-8
...