다음을 통해 공유


sqlsrv_rows_affected

최근 실행된 문에서 수정된 행 수를 반환합니다. 이 함수는 SELECT 문에서 반환된 행 수는 반환하지 않습니다.

구문

sqlsrv_rows_affected( resource $stmt)

매개 변수

$stmt: 실행된 문에 해당하는 문 리소스입니다.

반환 값

최근 실행된 문에서 수정된 행 수를 나타내는 정수입니다. 수정된 행이 없는 경우 0이 반환됩니다. 수정된 행 수에 대한 정보가 제공되지 않은 경우 -1이 반환됩니다. 수정된 행 수를 검색하는 동안 오류가 발생한 경우 false가 반환됩니다.

다음 예제에서는 UPDATE 문에서 수정된 행 수를 표시합니다. SQL Server와 AdventureWorks 데이터베이스가 로컬 컴퓨터에 설치되어 있다고 가정합니다. 명령줄에서 이 예제를 실행하면 모든 출력이 콘솔에 기록됩니다.

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

참고 항목

개념

설명서에 포함된 코드 예제 정보

관련 자료

API 참조(SQL Server Driver for PHP)
데이터 업데이트(SQL Server Driver for PHP)