php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28870 mysqli_bind_result doesn't work with cloned objects
Submitted: 2004-06-21 18:35 UTC Modified: 2004-08-07 13:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: rodolfo at rodsoft dot org Assigned: georg (profile)
Status: Wont fix Package: MySQL related
PHP Version: 5.0.0 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-06-21 18:35 UTC] rodolfo at rodsoft dot org
Description:
------------
If you pass to mysqli_bind_result a member variable of an object that will be cloned and modified multiple times, each modification happens in all cloned objects, as if they weren't cloned copies, but a single copy of the object.

Reproduce code:
---------------
<?
/* Say you have a database called 'test' with one table
   called 'test' that has one column called 'a' */
$db = new mysqli('localhost');
$db->select_db('test');
$stmt = $db->prepare('SELECT a FROM test');
$stmt->execute();
$result = new stdclass;
// Comment the next line to have the 'right' behaviour
$stmt->bind_result($result->a);
for($i = 0; $i<10; ++$i)
{
    $result->a = $i;
    $results[] = clone $result;
}
foreach($results as $result)
    echo $result->a,"\n";
$db->close();
?>

Expected result:
----------------
0
1
2
3
4
5
6
7
8
9

Actual result:
--------------
9
9
9
9
9
9
9
9
9
9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-21 23:53 UTC] rodolfo at rodsoft dot org
A possible 'hack' that I've found is to bind the result to another variable before cloning, and just after that restore the bind to the original variable. This way the result is the expected one. It seems that there's a problem of how mysqli_bind_result stores the variable information while it is being cloned.
 [2004-08-07 11:01 UTC] georg@php.net
The problem is that we use ZVAL_ADDREF for bind variables 
(otherwise we had to rebind before every execute/fetch, 
which would be a significant performance loss). 
 
clone makes a binary copy of this object (and copies also 
the reference). Not sure if it's a bug. 
 
Changed category and assigned it to helly :) 
 [2004-08-07 11:24 UTC] helly@php.net
$result->a / all bound variables are references. Hence the result shown is correct. What we need is either disallowing clone or finding a way to unbind the variables in clone and showing an error message in case someone wants to execute a query with a cloned statement before rebinding its columns.
 [2004-08-07 11:26 UTC] helly@php.net
$result->a / all bound variables are references. Hence the result shown is correct. What we need is either disallowing clone or finding a way to unbind the variables in clone and showing an error message in case someone wants to execute a query with a cloned statement before rebinding its columns.
 [2004-08-07 13:06 UTC] georg@php.net
see previous explanation. 
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Sep 28 17:03:37 2021 UTC