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

PDO::FETCH_BOTH (integer)

Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0..

Used with:

  • PDOStatement:: fetch
  • PDOStatement:: fetchAll

This param is a standard data fetch param. In other words, if fetchAll()/fetch() does not use any params, it returns data as if PDO::FETCH_BOTH was used.

This is a combination of: PDO::FETCH_ASSOC and PDO::FETCH_NAMED.
TIn case of more than one column with the same name, it returns:

  • separate entries with numeric keys (columns given as an index starting from 0, from left)
  • one entry only for duplicated named keys (columns expressed as names) - (one overwritten)

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_NUM and other fetch commands:
  • PDOStatement::fetchColumn - returns null
  • PDOStatement:: fetchObject - returns error:
    Fatal error: Class '4' not found

 

PDOStatement::fetch - named columns without duplicates (named columns are NOT overwritten)

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 FROM posts';
$sth = $dbh->prepare($sql);
$sth->execute();

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

output:

print_r:

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

var_dump:

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

PDOStatement::fetch - named columns with duplicates (named columns are overwritten)

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

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_NUM);
print_r($result);
var_dump($result);

output:

print_r:

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

var_dump:

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

PDOStatement::fetchAll - named columns without duplicates (named columns are NOT overwritten)

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 FROM posts';
$sth = $dbh->prepare($sql);
$sth->execute();

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

output:

print_r:

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

var_dump:

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

PDOStatement::fetchAll - named columns with duplicates (named columns are overwritten)

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

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_NUM);
print_r($result);
var_dump($result);

output:

print_r:

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

var_dump:

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