准备PDO语句和MS SQL


Prepared PDO Statement and MS SQL

我终于让我的CentOS盒子在网络上与我们的MS SQL盒子对话了,但现在我在运行一个准备好的PDO语句时遇到了问题。如果我从末尾删除:desc并放入变量名或单词本身,下面的语句就会起作用。MS SQL是不喜欢这类语句,还是我只是缺少了什么?

$cn = "AARONS";
$result = $pdo->prepare("SELECT * from CommonNameAddress where CommonName = :desc");
$result->execute(array(':desc' => $cn));

编辑

在执行一些错误检查后,返回的错误为:

Array (
 [0] => 22001
 [1] => 0
 [2] => [Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation (SQLExecute[0] at /builddir/build/BUILD/php-5.3.3/ext/pdo_odbc/odbc_stmt.c:254)
 [3] => 22001
)

最终需要WHERE部分之后的like而不是=

$result = $pdo->prepare("SELECT * from CommonNameAddress where CommonName like :name");
$result->execute(array(':name' => "%$cn%"));

也许可以检查您的退货,看看是否发生了错误?

if( ! $result = $pdo->prepare("SELECT * from CommonNameAddress where CommonName = :desc") ) {
  print_r( $pdo->errorInfo() );
} else if( !$result->execute(array(':desc' => $cn)) ) {
  print_r( $result->errorInfo() );
} else {
  //success
}