020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO::FETCH_UNIQUE
[MySQL PDO Const]

PDO::FETCH_UNIQUE  (integer)

Returns the results of calling the specified function, using each row's columns as parameters in the call.

Used with:

  • PDOStatement:: fetchAll  [more]

 

see also: PDO::FETCH_COLUMN

 

PDO::FETCH_UNIQUE in action

code:

$sql = "SELECT color, shape, weight FROM a_code";
$stmt = $dbh->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE,2);

print_r($result);
var_dump($result);

 

output - unique values column: color, column selected for array values: weight (2 - counting starts from 0, from left)

Array

(
    [red] => heavy
    [blue] => heavy
    [green] => light
)
 
array
      'red' => string 'heavy' (length=5)
      'blue' => string 'heavy' (length=5)
      'green' => string 'light' (length=5)
 

 

This constant shows some very peculiar behaviour.

  1. it selects unique values from column indexed as 0 (first from the left) and takes them for keys of result array
    • to get proper unique selector column - put desired column as first in query,
      e.g.: "SELECT shape, color FROM a_code" - unique column is "shape"
      to change to color as a unique column change query
      "SELECT color, shape FROM a_code" - unique column is "color"
  2. column number selection allows to select, which values are taken for result array values