fetch() vs fetch all()

Post on 22-Jan-2018

2798 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

Transcript

PDOStatements

by Nikkala Thomsonfor CIT 336, section 04

ArrayTemporary

Associates values with keys

Cannot INSERT, DELETE, or UPDATE

DatabasePermanent

Stored as tables with rows and labeled columns

Can INSERT, DELETE, or UPDATE

fetch() or fetchAll()

Fetches the next qualifying row

(Notice the capital )

Fetches all remaining qualifying rows

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

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:

fruit

name color

apple green

banana yellow

pear red

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

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

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.

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

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

<?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

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

fruit

name color

apple green

banana yellow

pear red

apple red

pear yellow

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

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

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

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

top related