【发布时间】:2017-08-25 22:48:11
【问题描述】:
考虑下表:
CREATE TABLE foo (a VARCHAR(8), b VARCHAR(20))
然后考虑以下准备好的语句:
SELECT count(*) FROM foo WHERE a LIKE ?
最后,考虑以下 PHP 行:
$stmt = odbc_prepare($dsn, $query); // Where $query is the above query
$filter = "%12345678%"; // Note: two wildcards and 8 real characters
odbc_execute($stmt, array($filter));
使用 Windows x86 上的 PHP 5.3.13(作为 WAMP 包的一部分)以及 Microsoft SQL Server ODBC 驱动程序版本 06.01.7601 和 Microsoft SQL Server Native Client 版本 10.50.4000,odbc_execute 失败,生成“字符串数据,右截断”错误。
如果相反,我将过滤器字符串直接替换到查询中,如下所示:
SELECT count(*) FROM foo WHERE a LIKE '%12345678%'
当然它工作得很好,因为LIKE 不应该导致那个错误。
这是我对准备好的语句的误解,还是 PHP 或 SQL Server 驱动程序中的错误?
【问题讨论】:
-
我认为你需要在
%123456..%周围加上单引号,所以将过滤器设置为$filter = "'%12345678%'"; -
看起来很合理但不起作用。
odbc_execute仍然返回false,但现在odbc_error(sqlstate) 和odbc_errormsg都是空的。 :-(
标签: php sql-server odbc