sqlsrv_rows_affected

Switch View :
ScriptFree
Microsoft Drivers for PHP for SQL Server Version 1.1 and 2.0
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

Other Resources

Community Content

AndreGB
Always FALSE
At this moment, sqlsrv_rows_affected is returning FALSE to me no matter whether tSQL is complete or not.

dp_miller
Problem when not passing $params to sqlsrv_query
Hi there

I've discovered some odd behaviour using the sample above. If the query string is formed as complete (i.e. without merging $params) and the third parameter to sqlsrv_query() is ommitted, then an error is returned by sqlsrv_rows_affected().

Has anyone else encountered this?