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_KEY_PAIR
[MySQL PDO Const]

PDO::FETCH_KEY_PAIR  (integer)

Fetch into an array where the 1st column is a key and the 2nd column the value.
Throws a PDOException if any but 2 columns are requested in the SQL.

Used with:

  • PDOStatement:: fetchAll  [more]

 

see also: PDO::FETCH_COLUMN

 

PDOStatement::setFetchMode works with fetch / fetchAll

code:

$sql = "SELECT color, shape FROM a_code";
$stmt = $dbh->query($sql);
$stmt->setFetchMode(PDO::FETCH_KEY_PAIR);
$result = $stmt->fetchAll();
//also works with: $result = $stmt->fetch();
print_r($result);
var_dump($result);

 

output 

Array

(
    [red] => tringle
    [blue] => square
    [green] => square
)
 
array
  'red' => string 'tringle' (length=7)
  'blue' => string 'square' (length=6)
  'green' => string 'square' (length=6)
 

 

PDOStatement::fetch()

code:

$sql = "SELECT color, shape FROM a_code";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_KEY_PAIR);
print_r($result);
var_dump($result);

 

output 

 

Array
(
    [red] => tringle
)
array
  'red' => string 'tringle' (length=7)
 
 

 

PDOStatement::fetchAll()

code:

$sql = "SELECT color, shape FROM a_code";
$stmt = $dbh->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($result);
var_dump($result);

 

output 

 

Array
(
    [red] => tringle
    [blue] => square
    [green] => square
)
array
  'red' => string 'tringle' (length=7)
  'blue' => string 'square' (length=6)
  'green' => string 'square' (length=6)