다음을 통해 공유


sqlsrv_rows_affected

PHP 드라이버 다운로드

최근 실행한 문에서 수정된 행 수를 반환합니다. 이 함수는 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);  
?>  

참고 항목

SQLSRV 드라이버 API 참조

설명서의 코드 예제 정보

데이터 업데이트(Microsoft Drivers for PHP for SQL Server)