Share via


sqlsrv_rows_affected

Returns the number of rows modified by the last statement executed. This function does not return the number of rows returned by a SELECT statement.

Syntax

sqlsrv_rows_affected( resource $stmt)

Parameters

$stmt: A statement resource corresponding to an executed statement.

Return Value

An integer indicating the number of rows modified by the last executed statement. If no rows were modified, zero (0) is returned. If no information about the number of modified rows is available, negative one (-1) is returned. If an error occurred in retrieving the number of modified rows, false is returned.

Example

The following example displays the number of rows modified by an UPDATE statement. 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 Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail 
         SET SpecialOfferID = ? 
         WHERE ProductID = ?";

/* Set parameter values. */
$params = array(2, 709);

/* Execute the statement. */
$stmt = sqlsrv_query( $conn, $tsql, $params);

/* Get the number of rows affected and display appropriate message.*/
$rows_affected = sqlsrv_rows_affected( $stmt);
if( $rows_affected === false)
{
     echo "Error in calling sqlsrv_rows_affected.\n";
     die( print_r( sqlsrv_errors(), true));
}
elseif( $rows_affected == -1)
{
      echo "No information available.\n";
}
else
{
      echo $rows_affected." rows were updated.\n";
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

See Also

Concepts

About Code Examples in the Documentation

Other Resources

SQLSRV Driver API Reference (Microsoft Drivers for PHP for SQL Server)

Updating Data (Microsoft Drivers for PHP for SQL Server)