Jump to content

Passing a parameter to an MSSQL Stored Procedure


bicky

Recommended Posts

Hello All

 

Can someone help me with this:

 

I am trying to create some PHP code to pass 2 variables to a stored procedure.

 

At the moment I have:

 

<?php

 

// Connect to MSSQL and select the database

 

$serverName = "(local)";

$connectionInfo = array( "Database"=>"ashley");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

 

if( $conn === false )

{

    echo "Could not connect.\n";

    die( print_r( sqlsrv_errors(), true));

}

 

/*Call the stored procedure with a named parameter*/

$tsql = 'EXEC CPS_ADD_NEW_USER @agentF = php,@agentS=test';

$stmt = sqlsrv_query($conn, $tsql);

 

 

/* Free statement and connection resources. */

sqlsrv_free_stmt( $stmt);

 

sqlsrv_close( $conn);

?>

 

Could anyone advise me what I need to be doing and where I am going wrong

 

Thanks in advance

 

Bicky

Link to comment
Share on other sites

OK, sorry for the lack of information.

 

In the first instance, I would like to know if I am going about this the correct way.  Is my syntax correct?

 

To summarise what I intend to do is:

 

Have an HTML form with 2 fields (forename & surname)

I the when to click a submit button - this will then call the PHP code and pass the forename to parameter1 and surname to parameter2 of the existing MS stored procedure

 

Does this help?

 

Thanks

 

Bicky

Link to comment
Share on other sites

Hello All

 

I decided to go about it in a slightly different manner...I changed the stored procedure to only need one parameter and used the following code:

 

<?php

 

// Connect to MSSQL and select the database

 

$serverName = "(local)";

$connectionInfo = array( "Database"=>"ashley");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

 

if( $conn === false )

{

    echo "Could not connect.\n";

    die( print_r( sqlsrv_errors(), true));

}

 

/*Call the stored procedure with a named parameter*/

$name = array($_POST['newuserfullname']);

$tsql = 'EXEC CPS_ADD_NEW_USER @agent = ?';

$stmt = sqlsrv_query($conn, $tsql,$name);

 

/* Free statement and connection resources. */

sqlsrv_free_stmt( $stmt);

 

sqlsrv_close( $conn);

?>

 

Thanks for the help!

 

:shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.