准备参数LIKE语句不能与SQLSRV一起使用

时间:2015-01-09 17:35:30

标签: php sql-server-2008

我正在使用SQLSRV 3.0 for PHP并使用MSSQL2008。

我遇到的问题是,我在查询中无法获得LIKE语句吗?

$connectionOptions = array( "Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$conn = sqlsrv_connect( $myServer, $connectionOptions);

$params = array(
    "$sID%",
    "$sUser%"
);
$sql = "select * from tbl where col2 LIKE ? or col2 LIKE ?";

$stmt = sqlsrv_query($conn, $sql, $params); 

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

问题与params数组中项目的设计有关,有什么建议吗?

此致,Jakim

2 个答案:

答案 0 :(得分:1)

将代码调整为以下内容:

$sql = "select * from tbl where col2 LIKE ? or col2 LIKE ?";
$params = array($sID."%", $sUser."%");
$stmt = sqlsrv_query( $conn, $sql, $params);

答案 1 :(得分:0)

尝试这样做:

$strSQL = "SELECT * FROM tbl WHERE col2 like '%' + CONVERT(NVARCHAR, ?) + '%' or col2 like '%' + CONVERT(NVARCHAR, ?) + '%' ";

$searchTerm = "<<some multibye characters>>";

$searchTerm = iconv('utf-8', 'utf-16le', $searchTerm);

$params = array( array($searchTerm, NULL, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)));