Skip to content

Latest commit

 

History

History
102 lines (84 loc) · 3.23 KB

File metadata and controls

102 lines (84 loc) · 3.23 KB
title sqlsrv_execute | Microsoft Docs
ms.custom
ms.date 01/19/2017
ms.prod sql
ms.prod_service connectivity
ms.reviewer
ms.technology connectivity
ms.topic conceptual
apiname
sqlsrv_execute
apitype NA
helpviewer_keywords
sqlsrv_exclude
executing queries
API Reference, sqlsrv_execute
ms.assetid 38331bc2-4391-4f9f-aa83-9873dad605a0
author David-Engel
ms.author v-daenge

sqlsrv_execute

[!INCLUDEDriver_PHP_Download]

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

Note

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

Syntax

  
sqlsrv_execute( resource $stmt)  

Parameters

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

Return Value

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

Example

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);  
?>  

See Also

SQLSRV Driver API Reference

About Code Examples in the Documentation

sqlsrv_query