Fetch Modes
See cursor constants for the
Basic Fetch Modes
PDO::FETCH_CLASS options
These modes are used to implement options when using
Single Result ModesThe following modes cannot be used with PDOStatement::fetchAll.
Special Behavior Flags for PDOStatement::fetchAllThe following special modes for multiple results only work with PDOStatement::fetchAll and do not work with some other fetch modes. Check the full documentation for details.
Handling of Duplicated Column NamesIt's possible for results to contain multiple columns that use the same name. For example, when joining 2 tables that both contain a column with the same name. Because PHP structures such as arrays and objects don't support multiple keys or properties that use the same name, the returned array or object will contain only 1 of the values using the same name. Which value is returned for a given duplicated name should be considered undefined. To avoid this issue, explicitly name columns using an alias. For example: SELECT table1.created_at AS t1_created_at,
table2.created_at AS t2_created_at
FROM table1
JOIN table2 ON table1.table2id = table2.id
See also Setting the Default Fetch Mode
It is possible to set default fetch mode for all queries using
The default fetch mode for a specific statement can be set using
PDOStatement::setFetchMode.
This affects reuse as a prepared statement and iteration (using
警告
PDOStatement::setAttribute cannot be used to set the default fetch mode. It only accepts driver specific attributes and silently ignores attributes that are not recognized. PDO::FETCH_DEFAULT (int)Available as of PHP 8.0.7.
This is a special value that uses the current default fetch mode for a
PDOStatement. It's specifically useful as the default
value for method parameters when extending
PDOStatement for use with
This value cannot be used with
PDO::FETCH_ASSOC (int)
上の例の出力は以下となります。
Array
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
PDO::FETCH_BOTH (int)This is the default fetch mode.
The column number starts at 0 and is determined by the result column order in the query, not (for example) the order columns are defined in the table.
上の例の出力は以下となります。
Array
(
[id] => 104,
[0] => 104,
[name] => Chris,
[1] => Chris,
[country] => Ukraine,
[2] => Ukraine
)
PDO::FETCH_NAMED (int)
For more information on handling of duplicated column names and alternatives, see the handling of duplicated names section above. The order in which duplicated values are returned should be considered undefined. There's no way to tell where each value came from. 上の例の出力は以下となります。
Array
(
[userid] => 109
[name] => Array
(
[0] => Toni
[1] => Chris
)
[country] => Germany
[referred_by] = 104
)
PDO::FETCH_NUM (int)
上の例の出力は以下となります。
Array
(
[0] => 104
[1] => Chris
[2] => Ukraine
)
PDO::FETCH_COLUMN (int)
If the specified column does not exist a ValueError will be thrown. 上の例の出力は以下となります。
Array
(
[0] => Chris
[1] => Jamie
[2] => Robin
)
Array
(
[0] => Ukraine
[1] => England
[2] => Germany
)
PDO::FETCH_KEY_PAIR (int)
上の例の出力は以下となります。
Array
(
[Chris] => Ukraine
[Jamie] => England
[Robin] => Germany
)
PDO::FETCH_FUNC (int)Specify a function to create the returned value. This mode can only be used with PDOStatement::fetchAll. The function receives the values as parameters. There's no way to retrieve the column name a given value was associated with. It is crucial to ensure that the column order in the query matches the order of parameters of the function.
上の例の出力は以下となります。
Array
(
[0] => Array
(
[col1] => 104
[col2] => SAM
[col3] => Ukraine
[customKey] => customValue
)
[1] => Array
(
[col1] => 105
[col2] => JAMIE
[col3] => England
[customKey] => customValue
)
[2] => Array
(
[col1] => 107
[col2] => ROBIN
[col3] => Germany
[customKey] => customValue
)
)
PDO::FETCH_OBJ (int)
See also PDOStatement::fetchObject and
上の例の出力は以下となります。
stdClass Object
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
PDO::FETCH_CLASS (int)Returns an object of a specified class. For additional behaviors see the option flags. If a property does not exist with the name of a returned column, it will be dynamically declared. This behavior is deprecated and will cause an error from PHP 9.0. See also PDOStatement::fetchObject. 上の例の出力は、 たとえば以下のようになります。
Constructor called with 0 args
Properties set when constructor called? Yes
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
PDO::FETCH_CLASSTYPE (int)
This fetch mode can only be used combined with
When this fetch mode is used, PDO will use the first returned column as the name of the class to return. If the specified class cannot be found, a stdClass object will be returned, without warning or error. 上の例の出力は、 たとえば以下のようになります。
Constructor called with 0 args
Properties set when constructor called? Yes
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
PDO::FETCH_PROPS_LATE (int)
This fetch mode can only be used combined with
When this fetch mode is used, the constructor will be called before the properties are set. 上の例の出力は、 たとえば以下のようになります。
Constructor called with 0 args
Properties set when constructor called? No
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
PDO::FETCH_SERIALIZE (int)警告
This feature has been DEPRECATED as of PHP 8.1.0. Relying on this feature is highly discouraged.
This fetch mode can only be used combined with
When this fetch mode is used, the specified class must be Serializable. 警告
This feature does not support a string that contains a complete serialized object (with serialize). 警告
This fetch mode does not call the constructor. 上の例の出力は、 たとえば以下のようになります。
Deprecated: TestEntity implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in Standard input code on line 2
Set up record (constructor called manually):
Constructor called with 0 args
Properties set when constructor called? No
Retrieve result:
Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in Standard input code on line 58
Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in Standard input code on line 59
object(TestEntity)#5 (4) {
["userid"]=>
int(200)
["name"]=>
string(4) "Seri"
["country"]=>
string(5) "Syria"
["referred_by_userid"]=>
NULL
}
PDO::FETCH_BOUND (int)This fetch mode cannot be used with PDOStatement::fetchAll.
This fetch mode does not directly return a result, but binds values to
variables specified with PDOStatement::bindColumn. The
called fetch method returns
上の例の出力は以下となります。 104 Chris Ukraine NULL 105 Jamie England NULL 107 Robin Germany Chris 108 Sean Ukraine NULL 109 Toni Germany NULL 110 Toni Germany NULL PDO::FETCH_INTO (int)This fetch mode cannot be used with PDOStatement::fetchAll. This fetch mode updates properties in the specified object. The object is returned on success. If a property does not exist with the name of a returned column, it will be dynamically declared. This behavior is deprecated and will cause an error from PHP 9.0.
Properties must be 警告
There's no way to change the object to be updated without using PDOStatement::setFetchMode between retrieving each record. 上の例の出力は、 たとえば以下のようになります。
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
PDO::FETCH_LAZY (int)This fetch mode cannot be used with PDOStatement::fetchAll.
This fetch mode returns a PDORow object which provides
both array- and object-like access to values (i.e. combines the behavior of
This can provide memory efficient access (on the PHP side) to unbuffered results on the database server. Whether PDO uses client-side buffering for results depends on the database-specific driver used (and its configuration). 警告
PDORow will return 警告
The returned PDORow object is updated each time a result is retrieved. 上の例の出力は以下となります。 ID: 104 Name: Chris Country: Ukraine Does not exist: NULL ID: 105 PDO::FETCH_GROUP (int)
When combined with
This fetch should be combined with one of
If no fetch mode from the above list is given, the current default fetch mode for the PDOStatement will be used. 上の例の出力は以下となります。
Array
(
[Ukraine] => Array
(
[0] => Array
(
[userid] => 104
[name] => Chris
)
[1] => Array
(
[userid] => 108
[name] => Sean
)
)
[England] => Array
(
[0] => Array
(
[userid] => 105
[name] => Jamie
)
)
[Germany] => Array
(
[0] => Array
(
[userid] => 107
[name] => Robin
)
[1] => Array
(
[userid] => 109
[name] => Toni
)
)
)
In the above example one should note that the first column is omitted from the array for each row, only available as the key. It can be included by repeating the column, as in the following example: 上の例の出力は以下となります。
Array
(
[Ukraine] => Array
(
[0] => Array
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
[1] => Array
(
[userid] => 108
[name] => Sean
[country] => Ukraine
)
)
[England] => Array
(
[0] => Array
(
[userid] => 105
[name] => Jamie
[country] => England
)
)
[Germany] => Array
(
[0] => Array
(
[userid] => 107
[name] => Robin
[country] => Germany
)
[1] => Array
(
[userid] => 109
[name] => Toni
[country] => Germany
)
)
)
PDO::FETCH_UNIQUE (int)
When combined with
This fetch should be combined with one of
If no fetch mode from the above list is given, the current default fetch mode for the PDOStatement will be used. When used with a column that is known to be unique (such as record ID), this mode provides the ability to quickly return results indexed by that value.
警告
Filtering records should be done in SQL where possible. The database will use indexes to optimize this process and return only the required records. Selecting more records than required from the database may significantly increase memory usage and query time for larger result sets. 上の例の出力は以下となります。
Array
(
[104] => Array
(
[name] => Chris
[country] => Ukraine
)
[105] => Array
(
[name] => Jamie
[country] => England
)
[107] => Array
(
[name] => Robin
[country] => Germany
)
)
In the above example one should note that the first column is omitted from the array for each row, only available as the key. It can be included by repeating the column, as in the following example: 上の例の出力は以下となります。
Array
(
[104] => Array
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
[105] => Array
(
[userid] => 105
[name] => Jamie
[country] => England
)
[107] => Array
(
[userid] => 107
[name] => Robin
[country] => Germany
)
)
|