php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60665 call to empty() on NULL result using PDO::FETCH_LAZY returns false
Submitted: 2012-01-05 14:56 UTC Modified: 2016-08-12 20:28 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: denaje at gmail dot com Assigned: cmb (profile)
Status: Closed Package: PDO Core
PHP Version: 5.3.8 OS: Ubuntu Linux / Windows 7
Private report: No CVE-ID: None
 [2012-01-05 14:56 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-02 10:28 UTC] 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']));
 [2013-06-12 04:00 UTC] ssufficool@php.net
-Summary: call to empty() on NULL result using PDO::FETCH_LAZY returns false +Summary: PDO_MYSQL: call to empty() on NULL result using PDO::FETCH_LAZY returns false
 [2014-01-01 12:41 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 [2016-08-12 19:58 UTC] cmb@php.net
-Status: Open +Status: Analyzed -Package: PDO MySQL +Package: PDO Core -Assigned To: +Assigned To: cmb
 [2016-08-12 19:58 UTC] 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>
 [2016-08-12 20:28 UTC] cmb@php.net
-Summary: PDO_MYSQL: call to empty() on NULL result using PDO::FETCH_LAZY returns false +Summary: call to empty() on NULL result using PDO::FETCH_LAZY returns false
 [2016-08-12 23:40 UTC] cmb@php.net
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
 [2016-08-12 23:40 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-10-17 10:09 UTC] bwoebi@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC