SQL Server Driver for PHP에서는 결과 집합 행의 단일 필드를 검색하는 sqlsrv_get_field 함수를 제공합니다. 이 항목의 예제에서는 한 번에 한 필드씩 데이터 행을 검색하는 방법을 보여 줍니다.
참고
반환되는 데이터의 PHP 데이터 형식을 지정하거나 데이터를 스트림으로 검색해야 하는 경우 sqlsrv_get_field 함수를 사용해야 합니다. 자세한 내용은 데이터 검색 함수 비교를 참조하십시오.
예
다음 예제에서는 sqlsrv_fetch 함수를 사용하여 결과 집합의 각 행을 반복 처리한 다음 sqlsrv_get_field 함수를 사용하여 행의 각 필드를 반복 처리합니다.
AdventureWorks 데이터베이스의 Purchasing.PurchaseOrderDetail 테이블에 있는 제품 정보에서 재고 수량(StockQty)이 지정된 값 미만이며 지정된 날짜를 갖는 제품을 검색합니다.
이 예제에서는 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));
}
/* Define the query. */
$tsql = "SELECT ProductID,
UnitPrice,
StockedQty
FROM Purchasing.PurchaseOrderDetail
WHERE StockedQty < 3
AND DueDate='2002-01-29'";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set, printing a row of data upon each
iteration. Note that fields must be accessed in ordinal order. */
while( sqlsrv_fetch( $stmt))
{
echo "ProdID: ".sqlsrv_get_field($stmt, 0)."\n";
echo "UnitPrice: ".sqlsrv_get_field($stmt, 1)."\n";
echo "StockedQty: ".sqlsrv_get_field($stmt, 2)."\n";
echo "-----------------\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
참고 항목
태스크
방법: 데이터를 배열로 검색
방법: 데이터를 개체로 검색