This page gives an overview about the methods that allow you to fetch entire result sets without looping over single result rows: getOne(), getRow(), getCol(), getAssoc() and getAll().
Our data source is the following table:
Table 39-1. Data used in this example
id | nickname | |
---|---|---|
1 | pinky | pinky@domination.org |
2 | thebrain | brain@dominators.net |
A simple var_dump() is used to make clear in which use cases the methods can be used:
<?php require_once 'DB.php'; $db = DB::connect('mysql://user:pass@localhost/example'); var_dump( $db->getOne('SELECT nickname, id, email FROM users') ); ?> |
getRow() fetches the first row.
array(3) { [0] => string(5) "pinky" [1] => string(1) "1" [2] => string(20) "pinky@domination.org" } |
getCol() fetches the first column of all rows.
array(2) { [0]=> string(5) "pinky" [1]=> string(8) "thebrain" } |
getAssoc() fetches all rows into an array, using the first colum of the rows as array key. This column is removed from the array itself.
array(2) { ["pinky"] => array(2) { [0] => string(1) "1" [1] => string(20) "pinky@domination.org" } ["thebrain"] => array(2) { [0] => string(1) "2" [1] => string(20) "brain@dominators.net" } } |
getAll() fetches all columns from all rows into an array.
array(2) { [0] => array(3) { [0] => string(5) "pinky" [1] => string(1) "1" [2] => string(20) "pinky@domination.org" } [1] => array(3) { [0] => string(8) "thebrain" [1] => string(1) "2" [2] => string(20) "brain@dominators.net" } } |