CakeFest 2024: The Official CakePHP Conference

sqlsrv_fetch_array

(No version information available, might only be in Git)

sqlsrv_fetch_array行を配列として返す

説明

sqlsrv_fetch_array(
    resource $stmt,
    int $fetchType = ?,
    int $row = ?,
    int $offset = ?
): array

クエリ結果から列データを順に返します。列データの型は連想配列、配列、またはその両方(デフォルト)を選択できます。

パラメータ

stmt

sqlsrv_query あるいは sqlsrv_prepare が返すステートメントリソース。

fetchType

戻り値の型を指定します。連想配列SQLSRV_FETCH_ASSOC, 配列SQLSRV_FETCH_NUMERIC, and 連想配列と配列の両方SQLSRV_FETCH_BOTH (デフォルト) のうち1つを指定します。

SQLSRV_FETCH_ASSOC(連想配列)は同じ名前の列が複数存在する結果セットを扱う際に使ってはいけません(連想配列のキーが重複するため)。

row

スクロール可能なカーソルを用いた結果セットから、取得する列を指定します。 定義済み定数は、 SQLSRV_SCROLL_NEXT(次の列), SQLSRV_SCROLL_PRIOR(前の列), SQLSRV_SCROLL_FIRST(最初の列), SQLSRV_SCROLL_LAST(最後の列), SQLSRV_SCROLL_ABSOLUTE(列番号の絶対指定) and, SQLSRV_SCROLL_RELATIVE(列番号の相対指定) (デフォルト値). この引数を指定する場合は、fetchType を指定しなくてはなりません。

offset

引数rowに SQLSRV_SCROLL_ABSOLUTE または SQLSRV_SCROLL_RELATIVE を指定する際に、この引数で取得する列を指定します。 列番号は0から始まります。

戻り値

成功時に配列を返します。 返す列がない場合はnull を返します。エラーが起きた場合は falseを返します。

例1 連想配列を扱う場合の例

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

例2 配列を扱う場合の例

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

注意

上の例で fetchType の指定を省略したり、定数 SQLSRV_FETCH_TYPE を明示的に指定した場合は、 連想配列と数値添字の両方のキーを含む配列を返します。

同じ名前のカラムが複数存在する場合は、最後に現れたカラムがそれまでのすべてを上書きします。 名前の衝突を回避するには、別名を指定しましょう。

無名のカラムが返された場合、連想配列のキーは空文字列("")になります。

参考

add a note

User Contributed Notes 3 notes

up
-2
albornozg dot rene at gmail dot com
5 years ago
Example with an iteration ( SQLSRV_SCROLL_ABSOLUTE ).

for ($i=0; $i < sqlsrv_num_rows($stmt); $i++) {

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_ABSOLUTE , $i );
echo "value of column 1: '.trim($row[0]).', value of column 2: '.trim($row[1]);

}
up
-18
dlyasaitov184 at yandex dot ru
6 years ago
When I try to use SQLSRV_FETCH_BOTH for SQL-statement about "select ... from [viewName]" result set contained superfluous fields( duplicates and other fields from joined tables). Other types of fetchType work correctly.
up
-25
Anonymous
9 years ago
Note that while the docs say to avoid SQLSRV_FETCH_ASSOC when dealing with result sets where multiple fields have the same name, there are cases when this is perfectly valid to do.

Consider the following query:

SELECT * FROM a INNER JOIN b ON a.id = b.id

For any row, if you fetch NUMERIC you'll get a field for both a.id and b.id, which probably isn't very useful.

If you fetch ASSOC, you'll get one field for "id", and given that it's always the same in both tables (because your query insists it is so), you're not at risk of losing anything.

If you're generating output based on an unknown number of fields, the ASSOC behavior might be preferred.
To Top