PDOStatement::fetch — Get the next row from the result set (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )
Gets the next row from the result set associated with a PDOStatement object. The fetch_style parameter determines how the POD returns rows.
fetch_style
Controls how the next line is returned to the caller. This value must be one of the PDO::FETCH_* series of constants, and the default is the value of PDO::ATTR_DEFAULT_FETCH_MODE (the default is PDO::FETCH_BOTH).
PDO::FETCH_ASSOC : Returns an array indexed by the result set column name
PDO::FETCH_BOTH (default): Returns an array indexed by the result set column name and the column number starting with 0
PDO::FETCH_BOUND : Returns TRUE
and assigns the column value in the result set to the PHP variable bound by the PDOStatement::bindColumn() method.
PDO::FETCH_CLASS : Returns a new instance of the requested class, mapping the column names in the result set to the corresponding attribute names in the class. If fetch_style
contains PDO::FETCH_CLASSTYPE (for example: PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE ), the class name is determined by the value of the first column
PDO::FETCH_INTO : Updates an existing instance of the requested class, mapping columns in the result set to attributes named in the class
PDO::FETCH_LAZY : Use PDO::FETCH_BOTH and PDO::FETCH_OBJ in combination to create an object variable name for access
PDO::FETCH_NUM : Returns an array indexed to the result set column number starting with 0
PDO::FETCH_OBJ : Returns an anonymous object whose attribute name corresponds to the column name of the result set
cursor_orientation For a scrollable cursor represented by a PDOStatement object, this value determines which row will be returned to the caller. This value must be one of the PDO::FETCH_ORI_* series of constants, and the default is PDO::FETCH_ORI_NEXT. To make the PDOStatement object use a scrollable cursor, you must set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when using PDO::prepare() to prepare the SQL statement.
offsetFor a scrollable cursor represented by a PDOStatement object with the cursor_orientation parameter set to PDO::FETCH_ORI_ABS, this value specifies the absolute row number of the row in the result set that is to be retrieved. For a scrollable cursor represented by a PDOStatement object with the cursor_orientation parameter set to PDO::FETCH_ORI_REL, this value specifies the position of the row to be fetched relative to the cursor before calling PDOStatement::fetch()
The value returned by this function (method) on success depends on the extraction type. In all cases, failure returns FALSE.
<?php$sth = $dbh->prepare("SELECT name, color FROM fruit");$sth->execute();/* Use PDOStatement::fetch style*/print("PDO::FETCH_ASSOC: ") ;print("Return next row as an array indexed by column namen");$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 numbern");$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 propertiesn");$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 propertiesn");$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 nameArray( [NAME] => apple [COLOUR] => red)PDO::FETCH_BOTH: Return next row as an array indexed by both column name and numberArray( [NAME ] => banana [0] => banana [COLOUR] => yellow [1] => yellow)PDO::FETCH_LAZY: Return next row as an anonymous object with column names as propertiesPDORow Object( [NAME] => orange [COLOUR] => orange)PDO::FETCH_OBJ: Return next row as an anonymous object with column names as propertieskiwi
<?phpfunction readDataForwards($dbh) { $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY BET'; try { $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO: :CURSOR_SCROLL)); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { $data = $row[0] . "t" . $row[1] . "t" . $row[2] . " n"; print $data; } $stmt = null; } catch (PDOException $e) { print $e->getMessage(); }}function readDataBackwards($dbh) { $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY bet'; try { $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) ); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); do { $data = $row[0] . "t" . $row[1] . "t" . $row[2] . "n"; print $data; } while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR)); $stmt = null; } catch (PDOException $e) { print $e->getMessage(); }}print "Reading forwards:n";readDataForwards($conn);print "Reading backwards :n";readDataBackwards($conn);?>
The above example will output:
Reading forwards:21 10 516 0 519 20 10Reading backwards:19 20 1016 0 521 10 5