字符串数据,SQLSRV PHP 驱动程序中的右截断(“甜甜圈洞”)

发布于 2024-10-06 00:40:27 字数 949 浏览 0 评论 0原文

我正在使用 Microsoft 的 sqlsrv PHP 驱动程序(在 Windows 2003 服务器上)将大量文本写入 text 列。

有一个奇怪的“甜甜圈洞”错误,如果文本的长度相当小,则保存成功,如果文本足够大,则也保存成功。在这之间,我们收到以下错误:

SQLSTATE: 22001
code: 0
message: [Microsoft][SQL Server Native Client 10.0]String data, right truncation 

下限(触发错误)似乎约为 4011 个字符,上限为 8024。数据库中还有其他一些列可能会使其超过 -两个边界(4096 和 8192)。

无论正在运行什么 SQL,都会发生这种情况。作为测试用例,我们运行了以下内容:

$connectionInfo = array("UID" => "REDACTED", "PWD" => "REDACTED", "Database"=>"REDACTED", "CharacterSet" => "UTF-8");

$conn = sqlsrv_connect("SQLSERVER", $connectionInfo);

$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(&$large_string));

if (!sqlsrv_execute($stmt))
    print_r(sqlsrv_errors());

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

我们发现另一个表现出相同行为的表(使用 varchar(max) 列)。

编辑:在对 varchar 列使用 UTF-8 编码时会发生这种情况,但在使用标准编码时不会发生这种情况。

I'm writing large-ish amounts of text to a text column using Microsoft's sqlsrv PHP driver (on a Windows 2003 server).

There's an odd "donut hole" error, where if the length of the text is fairly small, it is saved successfully, and if the text is sufficiently large, it is also save successfully. In between, we get the following error:

SQLSTATE: 22001
code: 0
message: [Microsoft][SQL Server Native Client 10.0]String data, right truncation 

The lower limit (to trigger the error) appears to be around 4011 characters, and the upper limit 8024. There are a few other columns in the database that might push it over the power-of-two boundary (4096 and 8192).

This happens regardless of the sql being run. As a test case, we ran the following:

$connectionInfo = array("UID" => "REDACTED", "PWD" => "REDACTED", "Database"=>"REDACTED", "CharacterSet" => "UTF-8");

$conn = sqlsrv_connect("SQLSERVER", $connectionInfo);

$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(&$large_string));

if (!sqlsrv_execute($stmt))
    print_r(sqlsrv_errors());

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

We've found another table (using varchar(max) columns) that exhibits the same behavior.

Edit: This occurs when using UTF-8 encoding with a varchar column, but not when using the standard encoding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-10-13 00:40:27

如果您在该参数中提供其他参数,该错误就会消失:

$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(array(&$large_string, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('UTF-8'));

这可能与它将 UTF-8 值传递到 varchar(相对于 nvarchar)列这一事实有关。

If you feed additional parameters in with the parameter, the bug disappears:

$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(array(&$large_string, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('UTF-8'));

This may be related to the fact that it is passing a UTF-8 value into a varchar (versus nvarchar) column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文