Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

sqlsrv_execute

Executes a previously prepared statement. See sqlsrv_prepare for information on preparing a statement for execution.

Note Note

This function is ideal for executing a prepared statement multiple times with different parameter values.


sqlsrv_execute( resource $stmt)

$stmt: A resource specifying the statement to be executed. For more information about how to create a statement resource, see sqlsrv_prepare.

A Boolean value: true if the statement was successfully executed. Otherwise, false.

The following example executes a statement that updates a field in the Sales.SalesOrderDetail table in the AdventureWorks database. The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. All output is written to the console when the example is run from the command line.

<?php
/*Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false)
{
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Set up the Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail 
         SET OrderQty = ( ?) 
         WHERE SalesOrderDetailID = ( ?)";

/* Set up the parameters array. Parameters correspond, in order, to
question marks in $tsql. */
$params = array( 5, 10);

/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
if( $stmt )
{
     echo "Statement prepared.\n";
}
else
{
     echo "Error in preparing statement.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
      echo "Statement executed.\n";
}
else
{
     echo "Error in executing statement.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.