|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2008-09-20 19:43 UTC] chsc at peytz dot dk
 Description:
------------
PDO::FETCH_PROPS_LATE only works if it specified in both the $stmt->setFetchMode() call and in the following $stmt->fetch().
Calling
  $stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'MyClass');
  $obj = $stmt->fetch();
yields the same result as
  $stmt->setFetchMode(PDO::FETCH_CLASS, 'MyClass');
  $obj$stmt->fetch();
In order to make it work you should specify FETCH_PROPS_LATE in both calls, i.e.
  $stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'MyClass');
  $obj = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE);
Reproduce code:
---------------
class Person {
    public $name = NULL;
    public function __construct() {
        var_dump($this->name);
    }
}
$db = new PDO("mysql:dbname=foo", "foo", "secret");
$db->exec("CREATE TABLE person (id INTEGER NOT NULL, name VARCHAR(100))");
$db->exec("INSERT INTO person (id, name) VALUES (1, 'Sven')");
$stmt1 = $db->query('SELECT * FROM person');
$stmt1->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Person');
$obj1 = $stmt1->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE);
var_dump($obj1->name);
$stmt1 = NULL;
$stmt2 = $db->query('SELECT * FROM person');
$stmt2->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Person');
$obj2 = $stmt2->fetch();
var_dump($obj2->name);
Expected result:
----------------
NULL
string(4) "Sven"
NULL
string(4) "Sven"
Actual result:
--------------
NULL
string(4) "Sven"
string(4) "Sven"
string(4) "Sven"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 14:00:01 2025 UTC | 
Suggested fix: Index: ext/pdo/pdo_stmt.c =================================================================== RCS file: /repository/php-src/ext/pdo/pdo_stmt.c,v retrieving revision 1.118.2.38.2.24.2.22 diff -u -8 -p -r1.118.2.38.2.24.2.22 pdo_stmt.c --- ext/pdo/pdo_stmt.c 12 Aug 2008 17:20:25 -0000 1.118.2.38.2.24.2.22 +++ ext/pdo/pdo_stmt.c 20 Sep 2008 19:44:43 -0000 @@ -902,24 +902,25 @@ static int do_fetch_opt_finish(pdo_stmt_ } /* }}} */ /* perform a fetch. If do_bind is true, update any bound columns. * If return_value is not null, store values into it according to HOW. */ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, enum pdo_fetch_type how, enum pdo_fetch_orientation ori, long offset, zval *return_all TSRMLS_DC) /* {{{ */ { - int flags = how & PDO_FETCH_FLAGS, idx, old_arg_count = 0; + int flags, idx, old_arg_count = 0; zend_class_entry *ce = NULL, *old_ce = NULL; zval grp_val, *grp, **pgrp, *retval, *old_ctor_args = NULL; int colno; if (how == PDO_FETCH_USE_DEFAULT) { how = stmt->default_fetch_type; } + flags = how & PDO_FETCH_FLAGS; how = how & ~PDO_FETCH_FLAGS; if (!do_fetch_common(stmt, ori, offset, do_bind TSRMLS_CC)) { return 0; } if (how == PDO_FETCH_BOUND) { RETVAL_TRUE; Inspired by the fix for bug 41971: http://cvs.php.net/viewvc.cgi/php-src/ext/pdo/pdo_stmt.c?r1=1.118.2.38.2.21&r2=1.118.2.38.2.22