PHP.nl

sqlsrv_fetch_array

sqlsrv_fetch_array

Returns a row as an array

array **sqlsrv_fetch_array** resource $stmt int $fetchType int $row int $offset

Returns the next available row of data as an associative array, a numeric array, or both (the default).

stmtA statement resource returned by sqlsrv_query or sqlsrv_prepare.

fetchType A predefined constant specifying the type of array to return. Possible values are , , and (the default). SQLSRV_FETCH_ASSOC``SQLSRV_FETCH_NUMERIC``SQLSRV_FETCH_BOTH

A fetch type of SQLSRV_FETCH_ASSOC should not be used when consuming a result set with multiple columns of the same name.

row Specifies the row to access in a result set that uses a scrollable cursor. Possible values are , , , , and, (the default). When this parameter is specified, the must be explicitly defined. SQLSRV_SCROLL_NEXT``SQLSRV_SCROLL_PRIOR``SQLSRV_SCROLL_FIRST``SQLSRV_SCROLL_LAST``SQLSRV_SCROLL_ABSOLUTE``SQLSRV_SCROLL_RELATIVE``fetchType

offset Specifies the row to be accessed if the row parameter is set to or . Note that the first row in a result set has index 0. SQLSRV_SCROLL_ABSOLUTE``SQLSRV_SCROLL_RELATIVE

Returns an array on success, null if there are no more rows to return, and false if an error occurs.

Voorbeeld: Retrieving an associative array.

<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT FirstName, LastName FROM SomeTable";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      echo $row['LastName'].", ".$row['FirstName']."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

Voorbeeld: Retrieving a numeric array.

<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT FirstName, LastName FROM SomeTable";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
      echo $row[0].", ".$row[1]."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

Not specifying the or explicitly using the constant in the examples above will return an array that has both associative and numeric keys. fetchType``SQLSRV_FETCH_TYPE

If more than one column is returned with the same name, the last column will take precedence. To avoid field name collisions, use aliases.

If a column with no name is returned, the associative key for the array element will be an empty string ("").

sqlsrv_connect``sqlsrv_query``sqlsrv_errors``sqlsrv_fetch