How to execute a stored procedure in php using sqlsrv and "?" style parameters

How to execute a stored procedure in php using sqlsrv and "?" style parameters

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why sqlsrv_query Fails with Stored Procedure Parameters and How to Fix It

Attempting to call a stored procedure using parameterized queries in PHP with the sqlsrv driver can feel counterintuitive. You know that standard queries work perfectly fine:

$sql = "select * from aTable where col1 = ? AND col2 = ?";
$result = sqlsrv_query($myConn, $sql, array($var1, $var2));

This approach effectively mitigates SQL injection risks and handles data types correctly. However, when you try to apply the same logic to a stored procedure by simply replacing variables with question marks in the call string—like MyDB.dbo.myProcedure ?, ?, ?—the operation fails silently. The result set is false, no errors are reported via sqlsrv_errors(), and the database takes no action.

The root cause lies in how the SQL Server driver interprets stored procedure calls versus standard SQL statements. When you pass a string like "MyDB.dbo.myProcedure ?, ?, ?" to sqlsrv_query or sqlsrv_prepare, the driver treats this as a raw T-SQL command. In the context of a stored procedure call, the question marks are not recognized as positional parameters in the same way they are for ad-hoc queries. Instead, the driver expects specific syntax to define the input arguments for the procedure itself.

To resolve this, you must explicitly name the parameters using the @ symbol within the procedure call string and map them to your PHP variables. The corrected approach looks like this:

$sql = "MyDB.dbo.myProcedure @param1, @param2, @param3";
$params = array(
    "@param1" => $var1,
    "@param2" => $var2,
    "@param3" => $var3
);
$result = sqlsrv_query($myConn, $sql, $params);

Notice the shift from an indexed array to an associative array. The keys in the $params array must match the names used after the @ symbol in the SQL string. This mapping tells the driver exactly which PHP value corresponds to which parameter placeholder defined by the database engine.

For developers who prefer the prepare and execute pattern, the solution follows a similar principle but requires careful attention to variable passing. You cannot simply pass an indexed array of variables; you must provide an associative array where the keys match the named parameters:

$sql = "MyDB.dbo.myProcedure @param1, @param2, @param3";

// Ensure $var1, $var2, $var3 are passed as an associative array
$params = array(
    "@param1" => &$var1, 
    "@param2" => &$var2,
    "@param3" => &$var3
);

$stmt = sqlsrv_prepare($myConn, $sql, $params);
if ($stmt) {
    // If output parameters are used, they must be passed via the second argument to execute()
    // or included in the initial params array depending on specific driver behavior.
    $result = sqlsrv_execute($stmt);
}

A critical detail often missed is the use of reference operators (&) when passing variables into prepared statements with sqlsrv. While sqlsrv_query accepts values directly, sqlsrv_prepare typically requires references to allow for output parameters or specific binding behaviors. If your stored procedure returns data via output parameters rather than a result set, you must define these in the $params array as well, ensuring their names match the OUTPUT keywords in the SQL definition.

There is also a trade-off to consider regarding performance and code clarity. Using named parameters makes the code more readable and self-documenting, as the key names often describe the data being passed. However, it introduces a higher cognitive load during debugging compared to positional parameters in raw queries. If your stored procedure signature is complex with many arguments, an associative array can become cumbersome. In such cases, some developers revert to string interpolation for the call itself while still maintaining strict input validation outside the query string to prevent injection.

Ultimately, the sqlsrv driver distinguishes between ad-hoc SQL statements and stored procedure invocations. While both benefit from parameterization to ensure security, the syntax for defining those parameters differs significantly. By switching to named parameters with an associative array, you align your PHP code with the expectations of the SQL Server engine, allowing both simple queries and complex stored procedures to be executed securely and reliably.

Our network