php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80604 Changed behaviour of mysqli_stmt::bind_result in PHP-7.4
Submitted: 2021-01-07 09:03 UTC Modified: 2021-01-07 14:06 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: devel at mindscan dot de Assigned:
Status: Verified Package: MySQLi related
PHP Version: 7.4.13 OS: Linux
Private report: No CVE-ID: None
 [2021-01-07 09:03 UTC] devel at mindscan dot de
Description:
------------
I recently updated from PHP-7.3 to PHP-7.4 and noticed a behaviour, that I was not expecting. The following database and script are a minimal example to demonstrate my problem:

```
CREATE TABLE `user` (
  `idUser` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  PRIMARY KEY (`idUser`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

INSERT INTO `user` VALUES (1,'Alice'),(2,'Eve'),(3,'Bob'),(4,'Rick');

CREATE TABLE `user_data` (
  `idUserData` int(11) NOT NULL AUTO_INCREMENT,
  `idUser` int(11) NOT NULL,
  `data` text NOT NULL,
  PRIMARY KEY (`idUserData`),
  KEY `idUser` (`idUser`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

INSERT INTO `user_data` VALUES (1,1,'likes to read'),(2,3,'plays in a band');
```

```
<?php
$mysqli = new mysqli (HOST, USER, PASSWORD, DATABASE);

$stmt = $mysqli->prepare('SELECT idUser, name FROM user');
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($idUser, $name);

$stmt2 = $mysqli->prepare('SELECT data FROM user_data WHERE idUser = ?');
while ($stmt->fetch())
{
    $stmt2->bind_param('i', $idUser);
    $stmt2->execute();
    $stmt2->store_result();
    $stmt2->bind_result($data);
    $stmt2->fetch();

    print $name . ' => ' . $data . '<br>';
}
?>
```

PHP-7.3 gives me the following output (that I am expecting):
```
Alice =>  likes to read
Eve =>
Bob =>  plays in a band
Rick =>
```

With PHP-7.4 I get this output:
```
Alice =>  likes to read
Eve =>  likes to read
Bob =>  plays in a band
Rick =>  plays in a band
```

The bind_result() or fetch() in the while loop do not clear $data on an empty sql result set. It still contains the old value from the previous iteration. In PHP-7.3 and lesser versions, the var was cleared.

Kind regards
David


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-07 14:06 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-01-07 14:06 UTC] cmb@php.net
Apparently, this regression has been introduced with the support
for typed properties[1].

[1] <https://github.com/php/php-src/commit/e219ec144ef6682b71e135fd18654ee1bb4676b4>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 17:01:35 2024 UTC