Esempio 2. Fetching rows with a scrollable cursor
<?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 example will output: Reading forwards:
21 10 5
16 0 5
19 20 10
Reading backwards:
19 20 10
16 0 5
21 10 5 |
|