Skip to content
Advertisement

Parameterising an IN clause in PHP with sqlsrv

I have a method which selects records based on an array of integers which works when specifically typed. However, when I try to replace that array with one passed in from the method parameter sqlsrv_query() returns false. I am sure this is something very simple, but the ‘obvious’ possibilities to me do not work.

Working Version

JavaScript

Not Working Version 1

JavaScript

Not Working Version 2

JavaScript

Not Working Version 3

JavaScript

Advertisement

Answer

You need to generate a dynamic list of placeholders (?) and inlcude it in the SQL statement. Also, always check the result from sqlsrv_connect() and sqlsrv_query() execution.

This example, based on the code in the question, is a possible solution to your problem:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement