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

PDO::FETCH_NUM (integer)

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

Used with:

  • PDOStatement:: fetch
  • PDOStatement:: fetchAll

Use to get array with numeric keys. This param does not loose repeating columns, as it indexes them as it reads them starting from 0 as first index.

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

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

output:

print_r:

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

var_dump:

array
  0=> string 'post 1' (length=6)
  1=> string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
  2=>  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 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
        (
            [0] => post 1
            [1] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            [2] =>  2012-02-13 04:10:26
        )
 
    [1] => Array
        (
            [0] => post 2
            [1] => 2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            [2] => 2012-02-13 04:10:26
        )
)
 

var_dump:

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