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

PDO::FETCH_NAMED (integer)

Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set.
If the result set contains multiple columns with the same name, PDO::FETCH_NAMED returns an array of values per column name.

Used with:

  • PDOStatement:: fetch
  • PDOStatement:: fetchAll

Use in place of PDO::FETCH_ASSOC which returns only a single value per column name, so if you have multiple columns with same name (e.g. when you use joins).

PDOStatement:: fetch returns one rowset only in form of an array - loop to get them all.

PDOStatement:: fetchAll returns all results (all rows) - each row is delivered in form of an array, similar to one delivered by PDOStatement:: fetch.

PDO::FETCH_LAZY and other fetch commands:
  • PDOStatement::fetchColumn - returns column null
  • PDOStatement:: fetchObject - returns error:
    Fatal error: Class '11' not found

PDOStatement::fetch 

note: both functions deliver result with similar, array structure. PDOStatement:fetchAll simply delivers them all, so no result looping is necessary as it is with PDOStatement:fetch. Of course PDOStatement:fetchAll array needs to be processed (usually by looping) to retrieve data for processing.

attention: there is duplicated column "entry_date", to show difference from PD::FETCH_ASSOC

code:

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);

$sql = 'SELECT entry_title, entry_body, entry_date, entry_date FROM posts';
$sth = $dbh->prepare($sql);
$sth->execute();

$result = $sth->fetch(PDO::FETCH_NAMED);
print_r($result);
var_dump($result);

output:

print_r:

Array
(
    [entry_title] => post 1
    [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [entry_date] => Array
                                      (
                                             [0] => 2012-02-13 04:10:26
                                             [1] => 2012-02-13 04:10:26
                                       )
)
 

var_dump:

array
  'entry_title' => string 'post 1' (length=6)
  'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
  'entry_date' => 
                  array
                               0 => string '2012-02-13 04:10:26' (length=19)
                               1 => string '2012-02-13 04:10:26' (length=19)
 

PDOStatement::fetchAll

note: both functions deliver result with similar, array structure. PDOStatement:fetchAll simply delivers them all, so no result looping is necessary as it is with PDOStatement:fetch. Of course PDOStatement:fetchAll array needs to be processed (usually by looping) to retrieve data for processing.

attention: there is duplicated column "entry_date", to show difference from PD::FETCH_ASSOC

code:

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);

$sql = 'SELECT entry_title, entry_body, entry_date, entry_date FROM posts';
$sth = $dbh->prepare($sql);
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_NAMED);
print_r($result);
var_dump($result);

output:

print_r:

Array
(
    [0] => Array
        (
            [entry_title] => post 1
            [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            [entry_date] =>  Array
                                      (
                                             [0] => 2012-02-13 04:10:26
                                             [1] => 2012-02-13 04:10:26
                                       )
        )
 
    [1] => Array
        (
            [entry_title] => post 2
            [entry_body] => 2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            [entry_date] =>  Array
                                      (
                                             [0] => 2012-02-13 04:10:26
                                             [1] => 2012-02-13 04:10:26
                                       )
        )
)
 

var_dump:

array
  0 => 
    array
      'entry_title' => string 'post 1' (length=6)
      'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      'entry_date' =>   
                  array
                               0 => string '2012-02-13 04:10:26' (length=19)
                               1 => string '2012-02-13 04:10:26' (length=19)
  1 => 
    array
      'entry_title' => string 'post 2' (length=6)
      'entry_body' => string '2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      'entry_date' =>  
                  array
                               0 => string '2012-02-13 04:10:26' (length=19)
                               1 => string '2012-02-13 04:10:26' (length=19)