Description

Fetches a row from a result set associated with a PDOStatement object. The
fetch_style parameter determines how PDO returns
the row.

Parameters

fetch_style

Controls how the next row will be returned to the caller. This value
must be one of the PDO::FETCH_* constants,
defaulting to value of PDO::ATTR_DEFAULT_FETCH_MODE
(which defaults to PDO::FETCH_BOTH).

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 0-indexed 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 PDOStatement::bindColumn()
method

PDO::FETCH_CLASS: returns a new instance of the
requested class, mapping the columns of the result set to named
properties in the class. If fetch_style
includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS |
PDO::FETCH_CLASSTYPE) then the name of the class is
determined from a value of the first column.

PDO::FETCH_INTO: updates an existing instance
of the requested class, mapping the columns of the result set to
named properties in the class

PDO::FETCH_LAZY: combines
PDO::FETCH_BOTH and PDO::FETCH_OBJ,
creating the object variable names as they are accessed

PDO::FETCH_NAMED: returns an array with the same
form as PDO::FETCH_ASSOC, except that if there are
multiple columns with the same name, the value referred to by that
key will be an array of all the values in the row that had that
column name

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

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

cursor_orientation

For a PDOStatement object representing a scrollable cursor, this
value determines which row will be returned to the caller. This value
must be one of the PDO::FETCH_ORI_* constants,
defaulting to PDO::FETCH_ORI_NEXT. To request a
scrollable cursor for your PDOStatement object, you must set the
PDO::ATTR_CURSOR attribute to
PDO::CURSOR_SCROLL when you prepare the SQL
statement with PDO::prepare().

offset

For a PDOStatement object representing a scrollable cursor for which
the cursor_orientation parameter is set to
PDO::FETCH_ORI_ABS, this value specifies the
absolute number of the row in the result set that shall be fetched.

For a PDOStatement object representing a scrollable cursor for which
the cursor_orientation parameter is set to
PDO::FETCH_ORI_REL, this value specifies the
row to fetch relative to the cursor position before
PDOStatement::fetch() was called.

Return Values

The return value of this function on success depends on the fetch type. In
all cases, FALSE is returned on failure.

/* or create an instance yourself and use it */$user= new user();$sth->setFetchMode( PDO::FETCH_INTO, $user);$sth->execute();$user= $sth->fetch( PDO::FETCH_INTO );$sth->closeCursor();print ($user->id);?>

Someone's already pointed out that PDO::CURSOR_SCROLL isn't supported by the SQLite driver. It's also worth noting that it's not supported by the MySQL driver either.

In fact, if you try to use scrollable cursors with a MySQL statement, the PDO::FETCH_ORI_ABS parameter and the offset given to fetch() will be silently ignored. fetch() will behave as normal, returning rows in the order in which they came out of the database.

It's actually pretty confusing behaviour at first. Definitely worth documenting even if only as a user-added note on this page.

What is even worse is that PDO::prepare will NOT throw an Exception when it fails to prepare the query, even when the error mode is set to throw Exceptions, and will instead return a Boolean False!

Not only do I consider this a poor design choice, but also its a real shame that this is not documented anywhere in the manual -- in fact the manual is not clear on what Attributes are supported by which drivers and which are not so developers are left to play a classic game of guess.

Be careful with fetch() when you use prepared statements and MySQL (I don`t know how it is with other databases). Fetch won`t close cursor and won`t let you send any other query, even if your result set has only one row, . If you use $statement->fetch(), you will also have to use $statement->closeCursor() afterwards, to be albe to execute another query.Alternatively you can use $statement->fetchAll() without $statement->closeCursor().

Because MySQL does not currently support the use of cursors, the $cursor_offset feature cannot work when using PDO to access a MySQL database.

If you are tring to arbitrarily access a specific record or group of records in a MySQL database recordset, you might want to consider using the LIMIT clause of the SELECT statement to achieve this e.g. LIMIT 5,3 to return just the 6th,7th & 8th records - 3 records starting at index 5 (which is the 6th record).

If you want to use PDO::FETCH_CLASS you need to set it up with setFetchMode first like so: $stmt->setFetchMode( PDO::FETCH_CLASS, 'classType', array( 'parameters to constructor' ); $object = $stmt->fetch( PDO::FETCH_CLASS );If you ommit this PHP will segfault.

Note that PDO::ATTR_STRINGIFY_FETCHES will NOT work for the MySQL driver. MySQL will always return strings because that is the behaviour of the core mysql PHP extension. See http://bugs.php.net/bug.php?id=44341