2016-10-10 105 views
0

考慮如下表所示子句: PHP ODBC:字符串數據,右截斷在準備好的聲明

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上(作爲WAMP包的一部分)使用PHP 5.3.13,並且Microsoft SQL Server ODBC驅動程序版本爲06.01.7601和Microsoft SQL Server Native Client版本爲10.50.4000,odbc_execute失敗,從而生成一個「String數據,正確的截斷「錯誤。

相反,如果我直接替代過濾字符串到查詢,像這樣:

SELECT count(*) FROM foo WHERE a LIKE '%12345678%' 

當然它工作得很好,因爲LIKE是不是永遠應該導致該錯誤。

這是我得到錯誤的預備語句,或PHP或SQL Server驅動程序中的錯誤?

+0

我認爲你需要周圍的'%123456單引號..%'所以使過濾器,比如'$過濾器=「%12345678%'」;' – RamRaider

+0

似乎是合理的,但沒有按沒有工作。 'odbc_execute'仍然返回'false',但現在'odbc_error'(sqlstate)和'odbc_errormsg'都是空的。 :-( –

回答

0

雙引號引起的問題。試試這個代碼:

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

第二個單引號放在 – RamRaider

+0

以上@RamRaider感謝您的更正 –

+0

我試過了,單引號添加到字符串中,'odbc_execute'返回'false','odbc_error'和'odbc_errormsg'是空字符串 –