020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO::CURSOR_FWDONLY / CURSOR_SCROLL
[MySQL PDO Const]

PDO::CURSOR_FWDONLY - or  - PDO::CURSOR_SCROLL  (integer)

Most common data access in php looks like this: query prepares data and after that data is being fetched row by row (if there are multiple rows), or by getting a single result row with data fetched from selected columns.
This is default way.
 
PDO allows for a bit more creative way to access data fetched from database.
 
For that we need to set PDO::ATTR_CURSOR when we prepare query (or should I say: prepare statement for execution - see: here) using driver_options array. 
More about how to use PDO::ATTR_CURSOR - see: here.
 
 

 

Default PHP data access.

Please note: default data access works with fetch() and fetchAll().

When you create your prepared statement without some special ways of access data, your query template preparation looks like this:

$sql = "SELECT entry_title, entry_body, status FROM posts";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_NUM);

 

This section: $dbh->prepare($sql) can take additional parameters in form driver options array.
So, I can write same query like this:
$sql = "SELECT entry_title, entry_body, status FROM posts";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
$result = $sth->fetch(PDO::FETCH_NUM);

 

Same result can be achived by using PDO::CURSOR_SCROLL (used in defined data access below) in its basic setup (using PDO::FETCH_ORI_NEXT as second param. in fetch()), which scrolling through results forward one by one according to query.

$sql = "SELECT entry_title, entry_body, status FROM posts";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sth->execute();
$result = $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);

 

PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY equals default setting, when you fetch data forward - row-by-row.

 

Defined data access.

Using this way you can access results in more various ways, e.g. backwards, given row, row away by some rows from current row etc.
Lets look at examples.

First of all we have to deal with ATTR_CURSOR, and set it to CURSOR_SCROLL e.g.:

$sql = "SELECT entry_title, entry_body, status FROM posts";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sth->execute();

Code above will be commpleted in table below for each or available option constants.

Now lets take a look at how we fetch results.

Here we have a few different ways to pre-program output.

PDO::FETCH_ORI_NEXT (integer)

Fetch the next row in the result set.  Same as default data accessed (notes: above)

rest of code for this constant:

$result = $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);

Actually it is supported in a way only. MySQL driver simply ignores this setting, and fetching data regular way.

PDO::FETCH_ORI_PRIOR (integer)

Fetch the previous row in the result set. 

Does not seem to be supported by MySQL driver (5.3.9)

PDO::FETCH_ORI_FIRST (integer)

Fetch the first row in the result set. 

Does not seem to be supported by MySQL driver (5.3.9)

PDO::FETCH_ORI_LAST (integer)

Fetch the last row in the result set.

Does not seem to be supported by MySQL driver (5.3.9)

PDO::FETCH_ORI_ABS (integer)

Fetch the requested row by row number from the result set. This value specifies the absolute number of the row in the result set that will be fetched. Row number is given as an additional (offset) parameter.

Does not seem to be supported by MySQL driver (5.3.9)

PDO::FETCH_ORI_REL (integer)

Fetch the requested row by relative position from the current position of the cursor in the result set. This value specifies the row to fetch relative to the cursor position before PDOStatement::fetch() was called. Row number is given as an additional (offset) parameter.