PHP マニュアル
前のページ |  | 次のページ

# PDOStatement::fetch

(no version information, might be only in CVS)

PDOStatement::fetch -- Fetches the next row from a result set

## Description

array PDOStatement::fetch ( [int fetch_style])

警告
この関数は、 実験的なステータスにあります。これは、この関数の 動作、関数名、ここで書かれていること全てがPHPの将来のバージョンで予告 なく変更される可能性があることを意味します。注意を喚起するとともに自分 のリスクでこの関数を使用して下さい。

Fetches a row from a result set associated with a PDOStatement object.

fetch_style can be one of the following values:

- PDO_FETCH_ASSOC: returns an array indexed by column name as returned in your result set

- PDO_FETCH_BOTH (default): returns an array indexed by both column name and column number as returned in your result set

- PDO_FETCH_BOUND: returns `TRUE` and assigns the values of the columns in your result set to the PHP variables to which they were bound with the PDO::bindParam() method

- PDO_FETCH_LAZY: combines PDO_FETCH_BOTH and PDO_FETCH_OBJ, creating the object variable names as they are accessed

- PDO_FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set

- PDO_FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0

例 1. Fetching rows using different fetch styles

`<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Exercise PDOStatement::fetch styles */
print("PDO_FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $sth->fetch(PDO_FETCH_ASSOC);
print_r($result);
print("\n");
print("PDO_FETCH_BOTH: ");
print("Return next row as an array indexed by both column name and number\n");
$result = $sth->fetch(PDO_FETCH_BOTH);
print_r($result);
print("\n");
print("PDO_FETCH_LAZY: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO_FETCH_LAZY);
print_r($result);
print("\n");
print("PDO_FETCH_OBJ: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO_FETCH_OBJ);
print $result->NAME;
print("\n");
?>`

上の例の出力は以下となります:

    PDO_FETCH_ASSOC: Return next row as an array indexed by column name
    Array
    (
        [NAME] => apple
        [COLOUR] => red
    )

    PDO_FETCH_BOTH: Return next row as an array indexed by both column name and number
    Array
    (
        [NAME] => banana
        [0] => banana
        [COLOUR] => yellow
        [1] => yellow
    )

    PDO_FETCH_LAZY: Return next row as an anonymous object with column names as properties
    PDORow Object
    (
        [NAME] => orange
        [COLOUR] => orange
    )

    PDO_FETCH_OBJ: Return next row as an anonymous object with column names as properties
    kiwi

前のページ | ホーム | 次のページ
PDOStatement::execute | 上に戻る | PDOStatement::fetchAll