Export (0) Print
Expand All

Comparing Data Retrieval Functions

The SQL Server Driver for PHP provides the following options for retrieving data from a result set:

  • sqlsrv_fetch_array: Retrieves the next row of data as a numerically indexed array, associative array, or both.
  • sqlsrv_fetch_object: Retrieves the next row of data as a PHP object.
  • sqlsrv_fetch/sqlsrv_get_field: When used in combination, retrieves a specified field from a row of data. The sqlsrv_fetch function makes the next row of a result set available for reading. The sqlsrv_get_field function retrieves data from the specified field of the current row.
    Cc296153.note(en-US,SQL.90).gifNote:
    The combination of sqlsrv_fetch and sqlsrv_get_field provides forward-only access to data.

The sqlsrv_fetch_array and sqlsrv_fetch_object functions load an entire row of a result set into script memory and return data according to the default PHP data types. (For information about default PHP types, see Default PHP Data Types.)

In contrast, the combination of sqlsrv_fetch/sqlsrv_get_field loads only one field of a result set row into script memory and allows PHP return type specification. (For information about how to specify the PHP return type, see How to: Specify PHP Data Types.) This combination of functions also allows data to be retrieved as a stream. (For information about retrieving data as a stream, see Retrieving Data as a Stream.)

Cc296153.note(en-US,SQL.90).gifNote:
When you use any of the functions mentioned above, avoid null comparisons as the criterion for exiting loops. Because sqlsrv functions return false when an error occurs, the following code could result in an infinite loop upon an error in sqlsrv_fetch_array:

/* This code could result in an infinite loop. It is recommended that

you do NOT use null comparisons as the criterion for exiting loops,

as is done here. */

do{

$result = sqlsrv_fetch_array($stmt);

} while( !is_null($result));

Community Additions

Show:
© 2014 Microsoft