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

PDO::FETCH_ASSOC (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 (e.g. joins), PDO::FETCH_ASSOC returns only a single value per column name.

Used with:

  • PDOStatement:: fetch
  • PDOStatement:: fetchAll

PDO::FETCH_ASSOC returns only a single value per column name.
To avoid loosing column, use PDO::FETCH_NAMED.

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_ASSOC and other fetch commands:
  • PDOStatement::fetchColumn - returns column #2 - most likely PDO evaluates entry to: 
    col. value present, non-zero (not first), first available (second col.)
  • PDOStatement:: fetchObject - returns error - invalid entry is evaluated to 2:
    Fatal error: Class '2' 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.

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_ASSOC);
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] => 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' => 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.

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_ASSOC);
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] => 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] => 2012-02-13 04:10:44
        )
)
 

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' => 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' => string '2012-02-13 04:10:44' (length=19)