Overview - get* methods

Overview - get* methods – Comparison of get* methods

Description

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:

Data used in this example
id nickname email
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')
);
?>

getOne

getOne() fetches the first column of the first row.


string(5) "pinky"

getRow

getRow() fetches the first row.


array(3) {
  [0] => string(5)  "pinky"
  [1] => string(1)  "1"
  [2] => string(20) "pinky@domination.org"
}

getCol

getCol() fetches the first column of all rows.


array(2) {
  [0]=> string(5) "pinky"
  [1]=> string(8) "thebrain"
}

getAssoc

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

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"
  }
}