bicky Posted November 10, 2010 Share Posted November 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/218285-passing-a-parameter-to-an-mssql-stored-procedure/ Share on other sites More sharing options...
trq Posted November 10, 2010 Share Posted November 10, 2010 Could anyone advise me what I need to be doing and where I am going wrong Not without a description of your problem. My guess? Strings need quotes within SQL. $tsql = "EXEC CPS_ADD_NEW_USER @agentF = 'php',@agentS='test'"; Quote Link to comment https://forums.phpfreaks.com/topic/218285-passing-a-parameter-to-an-mssql-stored-procedure/#findComment-1132613 Share on other sites More sharing options...
bicky Posted November 10, 2010 Author Share Posted November 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/218285-passing-a-parameter-to-an-mssql-stored-procedure/#findComment-1132626 Share on other sites More sharing options...
bicky Posted November 10, 2010 Author Share Posted November 10, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/218285-passing-a-parameter-to-an-mssql-stored-procedure/#findComment-1132660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.