【问题标题】:CENTOS PHP PDO ODBC MSSQL 2008 r2CENTOS PHP PDO ODBC MSSQL 2008 r2
【发布时间】:2013-12-13 21:07:00
【问题描述】:

2 天前,SQL Admin 将 MSSQL 从 2008 升级到 2008 R2。 在此升级之前,我使用 FreeTDS 的 MSSQL,但升级后我无法从存储过程中读取任何输出参数。所以我从 Microsoft Site for Linux 安装了官方 ODBC 驱动程序(我使用的是 CentoOS 64) 现在: 我可以连接,做一个简单的查询,但我无法从存储过程中捕获任何输出参数,也无法捕获查询输出:

select OutputString from tTableOutput where ID = 5 

OutputString 包含大型 xml。

我尝试了 PDO、ODBC,但都无法正常工作。 如果我使用 PDO:

$result = $db->query("select OutputString from tTableOutput with(nolock) where ID = 5");
foreach ($result as $row) {
    print_r($row);
}

我明白了:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[01004]: String data, right truncated: 0 [Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation (SQLFetchScroll[0] at /builddir/build/BUILD/php-5.3.3/ext/pdo_odbc/odbc_stmt.c:528)

如何增加长度限制?

如果我使用 ODBC:

$conn = odbc_connect($dataSource,$user,$password);
$sql = "select OutputString from tTableOutput where ID = 5";

$rs=odbc_exec($conn,$sql);
odbc_binmode ($rs, ODBC_BINMODE_PASSTHRU);
odbc_longreadlen ($rs, 0);

if (!$rs){
    exit("Error in SQL");
}
while (odbc_fetch_row($rs)){
    print_r(odbc_result($rs, "OutputString"));

}
odbc_close($conn);

我明白了:

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 4294967293 bytes)

为什么这个查询需要 4GB 内存?

感谢您的帮助

【问题讨论】:

    标签: php sql-server pdo odbc centos


    【解决方案1】:

    我猜(驱动程序管理器跟踪可能显示更多)驱动程序将结果列描述为 blob/clob/var(max) 类型,它将返回 4Gb 或 -1 的长度。如您所见,如果 PHP 决定尝试分配那么多,它将失败。我会说它是 PHP 中的一个缺陷。您可以尝试将 XML 列转换为 TEXT 类型,这可能会说服 PHP 它是一种 longdata 类型,而不是尝试将其保存在一个块中。在 Easysoft 驱动程序中,我们添加了一个标志来限制长类型的报告大小以避免这种情况,但这对 MS 驱动程序没有多大帮助。

    【讨论】:

      【解决方案2】:

      您可以尝试将值转换为 varchar(您选择的大小)。

      即 换行:

      select OutputString from tTableOutput where ID = 5 
      

      select cast(OutputString as varchar(255)) from tTableOutput where ID = 5 
      

      这将阻止查询报告 4GB 的大小。

      【讨论】:

        猜你喜欢
        • 2016-10-19
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-28
        • 2023-03-28
        • 2013-03-27
        • 2012-03-31
        相关资源
        最近更新 更多