php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69796 mysqli_stmt::fetch doesn't assign null values to bound variables
Submitted: 2015-06-11 02:34 UTC Modified: -
From: theodorejb at outlook dot com Assigned:
Status: Closed Package: MySQLi related
PHP Version: master-Git-2015-06-11 (snap) OS: Windows 10
Private report: No CVE-ID: None
 [2015-06-11 02:34 UTC] theodorejb at outlook dot com
Description:
------------
I used the MySQLi object-oriented API to prepare, execute, and bind results for a query using the latest PHP 7 snapshot for Windows (x86 non thread safe). While testing, I noticed a subtle yet serious bug:

If one row has a non-null value for a field, and the next row has a null value for the same field, the bound variable will not be set to null when mysqli_stmt::fetch is called (it will retain its non-null value from the previous row).

For example, if I have a users table with the following rows:

username | age
-------- | ----
user1    | 25
user2    | NULL

Fetching the first row correctly binds "user1" and 25 to the variables passed to mysqli_stmt::bind_result, but after calling mysqli_stmt::fetch a second time, the variables contain "user2" and 25 (rather than "user2" and NULL).

Test script:
---------------
$mysqli = new mysqli("localhost", "user", "password", "database");
$stmt = $mysqli->prepare("SELECT username, age FROM users");
$stmt->execute();
$stmt->bind_result($username, $age);

$stmt->fetch();
var_dump([$username, $age]); // ["user1", 25]

$stmt->fetch();
var_dump([$username, $age]); // ["user2", 25]
// ^ bug - array should contain ["user2", NULL]

$stmt->close();


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-11 05:38 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=141f0ca18cd0dd3846cd5e36e59a798e70fc6a6e
Log: Fixed Bug #69796 mysqli_stmt::fetch doesn't assign null values to bound variables
 [2015-06-11 05:38 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2015-06-23 18:04 UTC] ab@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=141f0ca18cd0dd3846cd5e36e59a798e70fc6a6e
Log: Fixed Bug #69796 mysqli_stmt::fetch doesn't assign null values to bound variables
 [2016-07-20 11:38 UTC] davey@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=141f0ca18cd0dd3846cd5e36e59a798e70fc6a6e
Log: Fixed Bug #69796 mysqli_stmt::fetch doesn't assign null values to bound variables
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC