19
PDOStatements by Nikkala Thomson for CIT 336, section 04

fetch() vs fetch all()

Embed Size (px)

Citation preview

Page 1: fetch() vs fetch all()

PDOStatements

by Nikkala Thomsonfor CIT 336, section 04

Page 2: fetch() vs fetch all()

ArrayTemporary

Associates values with keys

Cannot INSERT, DELETE, or UPDATE

DatabasePermanent

Stored as tables with rows and labeled columns

Can INSERT, DELETE, or UPDATE

Page 3: fetch() vs fetch all()

fetch() or fetchAll()

Page 4: fetch() vs fetch all()

Fetches the next qualifying row

Page 5: fetch() vs fetch all()

(Notice the capital )

Fetches all remaining qualifying rows

Page 6: fetch() vs fetch all()

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’;

Page 7: fetch() vs fetch all()

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:

Page 8: fetch() vs fetch all()

fruit

name color

apple green

banana yellow

pear red

Page 9: fetch() vs fetch all()

<?php$sth = $dbh->prepare("SELECT name, color FROM fruit");$sth->execute();

print("PDO::FETCH_NUM: ");print("Return next row as an array indexed by column number\n");$result = $sth->fetch(PDO::FETCH_NUM);print_r($result);print("\n");

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");

Page 10: fetch() vs fetch all()

PDO::FETCH_NUM: Return next row as an array indexed by column number

Array (

[0] => apple

[1] => green

)

PDO::FETCH_ASSOC: Return next row as an array indexed by column name

Array (

[name] => banana

[color] => yellow

)

PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number

Array (

[name] => pear

[0] => pear

[color] => red

[1] => red

)

0 apple

1 green

name banana

color yellow

name pear

0 pear

color red

1 red

Page 11: fetch() vs fetch all()

Same $fetch_style options as fetch() plus…

PDO::FETCH_COLUMN: Specify which column you want with the $fetch_argument parameter. Returns an array consisting of all values from a single column.

PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE: Returns only unique values of a single column.

PDO::FETCH_COLUMN|PDO::FETCH_GROUP: Returns an associative array grouped by the values of the specified column.

Page 12: fetch() vs fetch all()

<?php$sth = $dbh->prepare("SELECT name, color FROM fruit");$sth->execute();

/* Fetch all of the remaining rows in the result set */print("Fetch all of the remaining rows in the result set:\n");$result = $sth->fetchAll(PDO::FETCH_ASSOC);print_r($result);?>

Page 13: fetch() vs fetch all()

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

Page 14: fetch() vs fetch all()

<?php$sth = $dbh->prepare("SELECT name, color FROM fruit");$sth->execute();

/* Fetch all of the values of the first column */$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);var_dump($result);?>

array(3) {

[0] => string(5) “apple”

[1] => string(6) “banana”

[2] => string(4) “pear”

}

0 apple

1 banana

2 pear

Page 15: fetch() vs fetch all()

<?php$insert = $dbh-> prepare("INSERT INTO fruit(name, color) VALUES (?, ?)");$insert->execute(array('apple', 'red'));$insert->execute(array('pear', 'yellow'));

$sth = $dbh->prepare("SELECT name, color FROM fruit");$sth->execute();

/* Group values by the first column */var_dump($sth-> fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));?>

Page 16: fetch() vs fetch all()

fruit

name color

apple green

banana yellow

pear red

apple red

pear yellow

Page 17: fetch() vs fetch all()

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

Page 18: fetch() vs fetch all()

fetch() vs fetchAll()

Use $fetch_style to structure the results

Don’t 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

Page 19: fetch() vs fetch all()

"PDOStatement::fetch." PHP.net. Web. 10 Feb. 2016.

"PDOStatement::fetchAll." PHP.net. Web. 10 Feb. 2016.