Bug #60665 [Opn->Ana]: PDO_MYSQL: call to empty() on NULL result using PDO::FETCH_LAZY returns false
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: PDO_MYSQL: call to empty() on NULL result using
PDO::FETCH_LAZY returns false
-Status: Open
+Status: Analyzed
Type: Bug
-Package: PDO MySQL
+Package: PDO Core
Operating System: Ubuntu Linux / Windows 7
PHP Version: 5.3.8
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[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)