String data, right truncation in SQLSRV PHP Driver ("donut hole")
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);
sqlsr开发者_StackOverflow社区v_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.
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.
精彩评论