gergalyb
gergalyb

Reputation: 99

mssql+php prepared statements incorrect syntax using %

I have this little code snippet:

$sql .= " (cOriginalFilename LIKE %?%)";

and this is the full query:

Select  cMsgID, 
        cDocType, 
        cOriginalFilename, 
        cSubAddress1, 
        cRecipientID, 
        cSenderID, 
        cStatus, 
        cStatusDateTime
From    tMessages
Where   (cOriginalFilename LIKE %?%)

Exact error message:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '@P1'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '@P1'. ) )

As you can see it is making a prepared statement but it is giving me an invalid syntax error. When I remove the %s it works. How should I modify the syntax to work with the %s? I couldn't find any info on this topic.

note: This is php and mssql

Upvotes: 1

Views: 1063

Answers (2)

Siyual
Siyual

Reputation: 16917

You're close - the % wildcards just need to be quoted for this:

Select  cMsgID, 
        cDocType, 
        cOriginalFilename, 
        cSubAddress1, 
        cRecipientID, 
        cSenderID, 
        cStatus, 
        cStatusDateTime
From    tMessages
Where   (cOriginalFilename LIKE '%' + ? + '%')

Upvotes: 1

PHPhil
PHPhil

Reputation: 1540

The % signs need to go in the variable that you assign to the parameter, instead of in the query.

Upvotes: 1

Related Questions