sqlsrv_fetch_array
(No version information available, might only be in Git)
sqlsrv_fetch_array — Returns a row as an array
Description
$stmt
[, int $fetchType
[, int $row
[, int $offset
]]] ) : arrayReturns 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
, andSQLSRV_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, thefetchType
must be explicitly defined. -
offset
-
Specifies the row to be accessed if the row parameter is set to
SQLSRV_SCROLL_ABSOLUTE
orSQLSRV_SCROLL_RELATIVE
. Note that the first row in a result set has index 0.
Return Values
Returns an array on success, NULL
if there are no more rows to return, and
FALSE
if an error occurs.
Examples
Example #1 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);
?>
Example #2 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);
?>
Notes
Not specifying the fetchType
or explicitly using the
SQLSRV_FETCH_TYPE
constant in the examples above will
return an array that has both associative and numeric keys.
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 ("").
See Also
- sqlsrv_connect() - Opens a connection to a Microsoft SQL Server database
- sqlsrv_query() - Prepares and executes a query
- sqlsrv_errors() - Returns error and warning information about the last SQLSRV operation performed
- sqlsrv_fetch() - Makes the next row in a result set available for reading
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.sqlsrv-fetch-array.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.