Bug #60665 [Ana->Csd]: call to empty() on NULL result using PDO::FETCH_LAZY returns false

From: Date: Fri, 12 Aug 2016 23:40:59 +0000
Subject: Bug #60665 [Ana->Csd]: call to empty() on NULL result using PDO::FETCH_LAZY returns false
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203229@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60665&edit=1

 ID:                 60665
 Updated by:         cmb@php.net
 Reported by:        denaje at gmail dot com
 Summary:            call to empty() on NULL result using PDO::FETCH_LAZY
                     returns false
-Status:             Analyzed
+Status:             Closed
 Type:               Bug
 Package:            PDO Core
 Operating System:   Ubuntu Linux / Windows 7
 PHP Version:        5.3.8
 Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7938ebf6c1b302d3d1b1bfb798f1cf6f07e1e178
Log: Fix #60665: call to empty() on NULL result using PDO::FETCH_LAZY returns false


Previous Comments:
------------------------------------------------------------------------
[2016-08-12 19:58:51] cmb@php.net

The culprit is row_prop_exists()[1], which ignores the check_empty
parameter; if check_empty is true, the function has to actually
fetch the respective column value, and check whether it is empty.

<https://github.com/php/php-src/blob/PHP-7.0.10/ext/pdo/pdo_stmt.c#L2544>

------------------------------------------------------------------------
[2012-05-02 10:28:58] uw@php.net

This is not MySQL specific. Can be reproduced with SQLite.

$pdo = new PDO('sqlite::memory');

$statement = $pdo->prepare("SELECT NULL AS _something");
$statement->execute();
var_dump($row = $statement->fetch(PDO::FETCH_LAZY));
var_dump($row->_something);
var_dump(empty($row->_something));

$statement = $pdo->prepare("SELECT NULL");
$statement->execute();
var_dump($row = $statement->fetch(PDO::FETCH_ASSOC));
var_dump($row['_something']);
var_dump(empty($row['_something']));

------------------------------------------------------------------------
[2012-01-05 14:56:35] denaje at gmail dot com

Description:
------------
> PHP 5.3.8 (on both Ubuntu Linux and Windows 7)
> ./configure --with-pdo-mysql
> No changes to php.ini

When fetching rows from a MySQL database using PDO with the default fetch method 
FETCH_LAZY, the empty() function returns false on NULL values in the database. 
This behavior does not exist when using other fetch methods (such as FETCH_OBJ or 
FETCH_ASSOC).

Test script:
---------------
$row = $stmt->fetch(PDO::FETCH_LAZY);

echo $row->Address2."\n";             // RIGHT: outputs ''
var_dump($row->Address2);             // RIGHT: outputs 'NULL'
var_dump(!$row->Address2);            // RIGHT: outputs 'bool(true)'
var_dump(!!$row->Address2);           // RIGHT: outputs 'bool(false)'

echo empty($row->Address2)."\n";      // WRONG: outputs ''
var_dump(empty($row->Address2));      // WRONG: outputs 'bool(false)'
var_dump(empty($row['Address2']));    // WRONG: outputs 'bool(false)'

$var = $row['Address2'];
var_dump($var);                       // RIGHT: outputs 'NULL'
var_dump(empty($var));                // RIGHT: outputs 'bool(true)'

Expected result:
----------------
NULL
bool(true)
bool(false)
1
bool(true)
bool(true)
NULL
bool(true)

Actual result:
--------------
NULL
bool(true)
bool(false)

bool(false)
bool(false)
NULL
bool(true)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=60665&edit=1


Thread (4 messages)

« previous php.bugs (#203229) next »