다음을 통해 공유


방법: 데이터를 배열로 검색

SQL Server Driver for PHP에서는 데이터 행을 배열로 검색하는 sqlsrv_fetch_array 함수를 제공합니다. 이 항목에서는 행 데이터를 배열로 검색하는 두 가지 예제를 제공합니다.

  • 첫 번째 예제에서는 sqlsrv_fetch_array를 사용하여 데이터 행을 숫자로 인덱싱된 배열로 검색하는 방법을 보여 줍니다.

  • 두 번째 예제에서는 sqlsrv_fetch_array를 사용하여 데이터 행을 결합형 배열로 검색하는 방법을 보여 줍니다.

    참고

    기본적으로 sqlsrv_fetch_array는 숫자 키와 결합형 키가 모두 있는 배열을 검색합니다.

다음 예제에서는 결과 집합의 각 행을 숫자로 인덱싱된 배열로 검색합니다.

이 예제에서는 AdventureWorks 데이터베이스의 Purchasing.PurchaseOrderDetail 테이블에 있는 제품 정보에서 재고 수량(StockQty)이 지정된 값 미만이며 지정된 날짜를 갖는 제품을 검색하며,

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

/* 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.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
     echo "ProdID: ".$row[0]."\n";
     echo "UnitPrice: ".$row[1]."\n";
     echo "StockedQty: ".$row[2]."\n";
     echo "-----------------\n";
}

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

sqlsrv_fetch_array 함수는 항상 기본 PHP 데이터 형식에 따라 데이터를 반환합니다. PHP 데이터 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정 또는 방법: 단일 필드 검색을 참조하십시오.

이름이 없는 필드가 검색되는 경우 배열 요소의 결합형 키는 빈 문자열("")이 됩니다. 자세한 내용은 sqlsrv_fetch_array를 참조하십시오.

다음 예제에서는 결과 집합의 각 행을 결합형 배열로 검색합니다. 배열의 키는 결과 집합의 열 이름에 해당합니다.

이 예제에서는 AdventureWorks 데이터베이스의 Purchasing.PurchaseOrderDetail 테이블에 있는 제품 정보에서 재고 수량(StockQty)이 지정된 값 미만이며 지정된 기한(DueDate)을 갖는 제품을 검색하며,

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

/* 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 the returned array is an associative array with
 keys corresponding to column names of the result set. */
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
     echo "ProdID: ".$row['ProductID']."\n";
     echo "UnitPrice: ".$row['UnitPrice']."\n";
     echo "StockedQty: ".$row['StockedQty']."\n";
     echo "-----------------\n";
}

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

sqlsrv_fetch_array 함수는 항상 기본 PHP 데이터 형식에 따라 데이터를 반환합니다. PHP 데이터 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정 또는 방법: 단일 필드 검색을 참조하십시오.

참고 항목

개념

데이터 검색 함수 비교
설명서에 포함된 코드 예제 정보

관련 자료

데이터 검색
프로그래밍 가이드