php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46139 PDOStatement->setFetchMode() forgets FETCH_PROPS_LATE
Submitted: 2008-09-20 19:43 UTC Modified: 2008-10-10 16:58 UTC
From: chsc at peytz dot dk Assigned: johannes (profile)
Status: Closed Package: PDO related
PHP Version: 5.3.0alpha2 OS: Linux
Private report: No CVE-ID: None
 [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"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-20 19:46 UTC] chsc at peytz dot dk
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
 [2008-09-22 12:03 UTC] matt at mattfarina dot com
I've found this same problem in PHP 5.2.6.
 [2008-09-25 18:22 UTC] johannes@php.net
Patch looks fine on first sight, I'll test it and apply then.
 [2008-10-10 16:58 UTC] felipe@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Hello, I committed your patch and added a fix to fetchAll too.

Thanks for the patch!

Fixed in 5.2, 5.3 and HEAD.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC