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])

Warning

This function is EXPERIMENTAL. The behaviour of this function, the name of this function, and anything else documented about this function may change without notice in a future release of PHP. Use this function at your own risk.

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

fetch_style can be one of the following values:

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

The above example will output:

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