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

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

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:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.