Phil Gyford
Phil Gyford

Reputation: 14674

Passing parameters to SQL Server stored procedures with PHP PDO

I'm having trouble passing parameters to a stored procedure on SQL Server from PHP with PDO.

I can successfully perform a query on a procedure that requires no parameters, with code like this simplified version:

$dbConnection =  Craft::createComponent(array(
    'charset'           => 'utf8',
    'class'             => 'CDbConnection',
    'autoConnect'       => true,
));

$dbConnection->connectionString = 'sqlsrv:Server=MYSERVER;Database=My_Database';
$dbConnection->username = 'MyUsername';
$dbConnection->password = 'MyPasword';

$command = $dbConnection->createCommand(
    'MYSERVER.My_Database.My_Schema.my_Stored_Procedure'
);

$data = $command->queryAll();

This returns data fine.

But if the stored procedure requires parameters, I can't work out how to do that. I've tried replacing the createCommand line with something like:

$command = $dbConnection->createCommand(
    "MYSERVER.My_Database.My_Schema.my_Stored_Procedure 1, 'A Parameter', '', ''"
);

using the four parameters I've been given, that should work, but this gets me:

 CDbCommand failed to execute the SQL statement: SQLSTATE[IMSSP]: The active
 result for the query contains no fields.. The SQL statement executed was:
 MYSERVER.My_Database.My_Schema.my_Stored_Procedure 1, 'A Parameter', '', ''

I'm not familiar with the syntax for SQL Server stored procedures, never mind calling them from PHP, so I'm a bit stumped.

Upvotes: 2

Views: 415

Answers (1)

meda
meda

Reputation: 45500

If your stored procedure does not return anything you have to use SET NOCOUNT ON;

You can read more about it here:

https://msdn.microsoft.com/en-us/library/ms189837.aspx

Upvotes: 1

Related Questions