fetch() vs fetch all()

Download fetch() vs fetch all()

Post on 22-Jan-2018

2.761 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

  1. 1. PDOStatements by Nikkala Thomson for CIT 336, section 04
  2. 2. Array Temporary Associates values with keys Cannot INSERT, DELETE, or UPDATE Database Permanent Stored as tables with rows and labeled columns Can INSERT, DELETE, or UPDATE
  3. 3. fetch()orfetchAll()
  4. 4. Fetches the next qualifying row
  5. 5. (Noticethecapital ) Fetches all remaining qualifying rows
  6. 6. Indexed numeric tag (starting with 0) $car[0] = Ford; Associative string tag $car[model] = Mustang; Multidimensional multiple tags (each element is an array) $cars[1][color] = red;
  7. 7. PDO::FETCH_NUM: returns an array indexed by column number, starting at column 0 PDO::FETCH_ASSOC: returns an array indexed by column name PDO::FETCH_BOTH (default): returns an array indexed by both column name and number (twice as large) $fetch_style options include:
  8. 8. fruit name color apple green banana yellow pear red
  9. 9.
  10. 13. Fetch all of the remaining rows in the result set: Array { [0] => Array ( [name] => apple [color] => green ) [1] => Array ( [name] => banana [color] => yellow ) [2] => Array ( [name] => pear [color] => red ) ) name apple color green name banana color yellow name pear color red 0 1 2
  11. 14. array(3) { [0] => string(5) apple [1] => string(6) banana [2] => string(4) pear } 0 apple 1 banana 2 pear
  12. 15.
  13. 16. fruit name color apple green banana yellow pear red apple red pear yellow
  14. 17. array(3) { [apple] => array(2) { [0] => string(5) green [1] => string(3) red } [banana] => array(1) { [0] => string(5) yellow } [pear] => array(2) { [0] => string(3) red [1] => string(5) green } } apple banana pear 0 green 1 red 0 yellow 0 red 1 yellow
  15. 18. fetch() vs fetchAll() Use $fetch_style to structure the results Dont use the default BOTH if you can use NUM or ASSOC When using fetchAll(), try to retrieve only the data you need to conserve system resources
  16. 19. "PDOStatement::fetch." PHP.net. Web. 10 Feb. 2016. "PDOStatement::fetchAll." PHP.net. Web. 10 Feb. 2016.