sqlsrv_fetch_array

(No version information available, might only be in Git)
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).

Parameters:
stmt

A statement resource returned by sqlsrv_query or sqlsrv_prepare.

fetchType

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

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 SQLSRV_SCROLL_NEXT, SQLSRV_SCROLL_PRIOR, SQLSRV_SCROLL_FIRST, SQLSRV_SCROLL_LAST, SQLSRV_SCROLL_ABSOLUTE and, SQLSRV_SCROLL_RELATIVE (the default). When this parameter is specified, the fetchType must be explicitly defined.

offset

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

Returns:

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

Examples:
Retrieving an associative array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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);
?>
Retrieving a numeric array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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);
?>
See also:

sqlsrv_connect() -

sqlsrv_query() -

sqlsrv_errors() -

sqlsrv_fetch() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.