msphpsql icon indicating copy to clipboard operation
msphpsql copied to clipboard

Error trying to run parameterized queries on Always Encrypted DB connection

Open tm8747a opened this issue 8 years ago • 1 comments

Driver version or file name

5.1.2-preview+10320

SQL Server version

14.0.3006.16

Client operating system

Windows 7

PHP version

7.0.4

Microsoft ODBC Driver version

ODBC Driver 17 for SQL Server

Problem description

I'm testing Always Encrypted and noticed the following error when trying to run a query with any kind of bound parameter: "Must specify the SQL type for each parameter in a parameterized query when using sqlsrv_query in a column encryption enabled connection." Is this always going to be so or is this something affected drivers because they are still in preview. Just checking because this will be a deal breaker for Always Encrypted for us, refactoring our entire app to specify parameter type everywhere in just not feasible.

Expected behavior and actual behavior

Currently, any parameterized query throws an error if parameter type is not specified when column encryption is on. Would expect it to be able to behave like normal connection, where type is optional.

Repro code

Just run any query doing sqlsrv_query($conn, 'SELECT * from table_name WHERE col_name = ?', ['val']) on a connection with 'ColumnEncryption'=>'Enabled' .

tm8747a avatar Jan 10 '18 15:01 tm8747a

@tm8747a , this is a known limitation (please see here). This limitation is in place because with Always Encrypted, the SQL Server does very little datatypes conversion. When binding parameters, the SQL Server has to know the exact datatype. The SQLSRV driver is designed to get the parameter datatype from the database and then perform bind parameter. However, for the driver to find the datatype, the statement must be prepared. One thing you can do when you refactor your app is to use sqlsrv_prepare and sqlsrv_execute instead. Using sqlsrv_prepare will not require the user to specify the SQLtype.

yukiwongky avatar Jan 10 '18 20:01 yukiwongky