YouTip LogoYouTip

Pdostatement Fetch

[![Image 1: PHP PDO Reference Manual](#)PHP PDO Reference Manual](#) PDOStatement::fetch β€” Fetches the next row from a result set (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0) * * * ## Description ### Syntax mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] ) Fetches the next row from a result set associated with a PDOStatement object. The fetch_style parameter determines how POD returns the row. * * * ## Parameters **fetch_style** Controls how the next row is returned to the caller. This value must be one of the PDO::FETCH_* series constants, with a default value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH). * _PDO::FETCH_ASSOC_: Returns an array indexed by result set column name. * _PDO::FETCH_BOTH_ (default): Returns an array indexed by both result set column name and a zero-based column number. * _PDO::FETCH_BOUND_: Returns **`TRUE`** and assigns the column values from the result set to the PHP variables bound by the PDOStatement::bindColumn() method. * _PDO::FETCH_CLASS_: Returns a new instance of the requested class, mapping result set column names to corresponding property names in the class. If `fetch_style` includes PDO::FETCH_CLASSTYPE (e.g., _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 result set columns to named properties in the class. * _PDO::FETCH_LAZY_: Combines the use of _PDO::FETCH_BOTH_ and _PDO::FETCH_OBJ_, creating object variable names for access. * _PDO::FETCH_NUM_: Returns an array indexed by a zero-based result set column number. * _PDO::FETCH_OBJ_: Returns an anonymous object with property names that correspond to the result set column names. **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 constants, with a default value of PDO::FETCH_ORI_NEXT. To make a PDOStatement object use a scrollable cursor, you must set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when preparing the SQL statement with PDO::prepare(). **offset** For a scrollable cursor represented by a PDOStatement object where the cursor_orientation parameter is set to PDO::FETCH_ORI_ABS, this value specifies the absolute row number in the result set of the row to be fetched. For a scrollable cursor represented by a PDOStatement object where the cursor_orientation parameter is set to PDO::FETCH_ORI_REL, this value specifies the row to be fetched relative to the cursor's position before calling PDOStatement::fetch(). * * * ## Return Value This function (method) returns a value dependent on the fetch type upon success. In all cases, it returns FALSE on failure. * * * ## Examples ### Fetching rows with different fetch styles prepare("SELECT name, colour FROM fruit"); $sth->execute();/* Using PDOStatement::fetch styles */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 name Array( => apple => red ) PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number Array( => banana => banana => yellow => yellow ) PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties PDORow Object( => orange => orange ) PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties kiwi ### Fetching rows using a scrollable cursor prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { $data = $row . "t" . $row . "t" . $row . "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 . "t" . $row . "t" . $row . "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 * * PHP PDO Reference Manual](#)
← Pdostatement FetchallPdostatement Execute β†’