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

PDO::FETCH_LAZY (integer)

Specifies that the fetch method shall return each row as an object, as they come with variable names that correspond to the column names returned in the result set. PDO::FETCH_LAZY creates the object variable names as they are accessed (no buffering). 

Used with:

  • PDOStatement::fetch

BTW: PDO::FETCH_LAZY  is largely responsible for lightning speed of Yii framework.

Important!

PDOStatement::fetch will not close query, so you will not be able to send another query.

Ways around it:

  • use PDOStatement::fetchAll
  • use PDOStatement::fetch and PDOStatement::closeCursor after that
  • use PDOStatement::setAttribute to force buffering, e.g. 
    $dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

code:

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

$result = $sth->fetch(PDO::FETCH_LAZY);
//sample output 1
print_r($result);
//sample output 2
var_dump($result);

output:

print_r:

PDORow Object
(
    [queryString] => SELECT entry_title, entry_date FROM posts
    [entry_title] => post 1
    [entry_date] => 2012-02-13 04:10:26
)

var_dump:

object(PDORow)[3]
  public 'queryString' => string 'SELECT entry_title, entry_date FROM posts' (length=41)
  public 'entry_title' => string 'post 1' (length=6)
  public 'entry_date' => string '2012-02-13 04:10:26' (length=19)

PDO::FETCH_LAZY and other fetch commands:

  • PDOStatement:: fetchAll - does not work - error returned
    General error: PDO::FETCH_LAZY can't be used with PDOStatement::fetchAll()
  • 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 1:
    Fatal error: Class '1' not found