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

PDO::FETCH_OBJ (integer)

Specifies that the fetch method shall return each row as an anonymous object (stdClass) with property names that correspond to the column names returned in the result set.

Used with:

  • PDOStatement:: fetch
  • PDOStatement:: fetchAll

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

PDOStatement:: fetchAll returns all results (all rows) - each row is delivered in form of an array of objects, 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 '5' not found

 

PDOStatement::fetch 

note: both functions deliver result with similar, array structure. PDOStatement:fetchAll simply delivers them all in form of array of objects, 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_OBJ);
print_r($result);
var_dump($result);

output:

print_r:

stdClass Object
(
    [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:

object(stdClass)[3]
    public 'entry_title' => string 'post 1' (length=6)
    public 'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
    public '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 in form of array of objects, 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_OBJ);
print_r($result);
var_dump($result);

output:

print_r:

Array
(
    [0] => stdClass Object
        (
            [entry_title] => post 1
            [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            [entry_date] => 2012-02-13 04:10:26
        )
 
    [1] => stdClass Object
        (
            [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 => 
    object(stdClass)[3]
      public 'entry_title' => string 'post 1' (length=6)
      public 'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      public 'entry_date' => string '2012-02-13 04:10:26' (length=19)
  1 => 
    object(stdClass)[4]
      public 'entry_title' => string 'post 2' (length=6)
      public 'entry_body' => string '2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      public 'entry_date' => string '2012-02-13 04:10:44' (length=19)