准备好的参数 LIKE 语句不适用于 SQLSRV

Prepared params LIKE statement Not working with SQLSRV

我正在为 PHP 使用 SQLSRV 3.0 并使用 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数组中item的设计有关,有什么建议吗?

此致,贾基姆

尝试这样做:

$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)));

将您的代码调整为以下内容:

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