【问题标题】:PHP ODBC: String Data, Right Truncation from LIKE clause in prepared statementPHP ODBC:字符串数据,从准备好的语句中的 LIKE 子句右截断
【发布时间】: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


【解决方案1】:

双引号可能导致问题。试试这个代码:

$stmt = odbc_prepare($dsn, $query);
$filter = array();
$filter[] = "'%12345678%'";          // single quotes around the entire LIKE 
odbc_execute($stmt, $filter);

【讨论】:

  • 第二个单引号在上面放错了
  • 我试过了。将单引号添加到字符串后,odbc_execute 返回 falseodbc_errorodbc_errormsg 都是空字符串。
  • @Lotharyx 你能试试我更新的答案吗?如果不行,我就删了。
  • 这不会出错,但也不会返回任何结果。看起来您在更新的答案中创建了一个数组数组,这对于调用 odbc_execute 是不正确的。
  • @Lotharyx 抱歉,再试一次 :-)
猜你喜欢
  • 2019-02-13
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多