PDOStatement :: fetch
PDOStatement :: fetch - focus on results obtained from the next row (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)
Explanation
grammar
mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )
From a PDOStatement object associated result set gets the next row. fetch_style POD parameter determines how the rows are returned.
parameter
fetch_style
How to control the next line is returned to the caller. This value must be PDO :: FETCH_ * constants in a series, the default value for the PDO :: ATTR_DEFAULT_FETCH_MODE (default PDO :: FETCH_BOTH).
PDO :: FETCH_ASSOC: returns an array indexed column names of the result set
PDO :: FETCH_BOTH (default): returns an index array result set column names beginning with 0 and column number
PDO :: FETCH_BOUND: returns
TRUE
, PHP variables and assign values to the columns in the result set PDOStatement :: bindColumn () method binding.PDO :: FETCH_CLASS: a request to return a new instance of the class, mapping column names in the result set to the class name of the corresponding property. 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 columnPDO :: FETCH_INTO: Update an existing request class instance mapped to columns in the result set class named property
PDO :: FETCH_LAZY: in conjunction with PDO :: FETCH_BOTH and PDO :: FETCH_OBJ, for creating an object variable name used to access
PDO :: FETCH_NUM: a return to the zero-indexed column number of result set array
PDO :: FETCH_OBJ: Returns a property name corresponding to the result set column names anonymous objects
cursor_orientation
For a PDOStatement object representing a scrollable cursor, this value determines which row is returned to the caller. This value must be PDO :: FETCH_ORI_ * constants in a series, the default is PDO :: FETCH_ORI_NEXT. To get PDOStatement object using scrollable cursors, you must use when PDO :: prepare () pretreatment SQL statements, set the PDO :: ATTR_CURSOR attribute PDO :: CURSOR_SCROLL.
offset
For a cursor_orientation parameter is set to PDO :: PDOStatement object representing a scrollable cursors FETCH_ORI_ABS, this value specifies the result set you want to get the absolute line number line.
For a cursor_orientation parameter is set to PDO :: FETCH_ORI_REL the PDOStatement object representing a scrollable cursor, this value specifies the call PDOStatement :: fetch () before the cursor position with respect to the line you want to get
return value
Back when this function (method) value depends on the successful extraction type. In all cases, failure returns FALSE.
Examples
Get row with different extraction methods
<?php $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* 运用 PDOStatement::fetch 风格 */ 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 examples 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
Use a scrollable cursor fetch rows
<?php function 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 examples will output:
Reading forwards: 21 10 5 16 0 5 19 20 10 Reading backwards: 19 20 10 16 0 5 21 10 5