0

I have a problem getting a SQL Server stored procedure to work using PHP. My code is below. The stored procedure is expecting an integer to be passed to it. The message that displays is "Stored procedure error". This shows the connection is working. I'm not sure what's wrong. Can someone help me out? Thanks.

$db_connection = sqlsrv_connect($servername, $connectOpts);

if($db_connection === false) {
     echo ("Error connecting to the database.");
    exit;
}

$sp_command = "EXEC usp_testsp @valint=?";
$sp_vals = array( array("3",    SQLSRV_PARAM_IN));
$sp_execute = sqlsrv_query($db_connection, $sp_command, $sp_vals);

if($sp_execute === false) {echo('Stored procedure error');}
else {echo('Success');}
sqlsrv_close($db_connection);

1 Answer 1

1

I don't see anything wrong with your code so I think your problem might be in your database itself. I can think of two possibilities.

  1. Your stored procedure is written incorrectly. If that's the case you should post the stored procedure here so we can look it over.
  2. Your stored procedure security settings aren't correct. You need to set the security settings inside SQL Server Management Studio. Off hand, I can't remember the exact path to set the security settings but it's something like: Right click on usp_testsp - properties - security. Then set the stored procedure with the database login information.
Sign up to request clarification or add additional context in comments.

2 Comments

The problem was the permissions. This is my first stored procedure attempt so I never even thought about that. Thanks.
No problem. I ran into the same issue when I first started working with stored procedures. The first one is always the hardest. Bryce, thanks for fixing the formatting of my answer. I just joined here and haven't learned the basic formatting tricks yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.