note 100945 deleted from mysqli-stmt.fetch by danbrown
| From: | danbrown@php.net | Date: | Tue, 16 Nov 2010 16:07:57 +0000 |
| Subject: | note 100945 deleted from mysqli-stmt.fetch by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174090@lists.php.net to get a copy of this message | ||
Note Submitter: daniel dot cox at phosphorix dot co dot uk
----
"piedone at pyrocenter dot hu" said that doing something like :
<?php
$stmt = $mysqli->prepare("SELECT id, name, url FROM dummy_table WHERE id=1 OR id=2");
$stmt->execute();
$stmt->bind_result($result['id'],$result['name'],$result['url']);
while($stmt->fetch()){
$results[]=$result;
}
var_dump($results);
?>
Will cause the array ($results) to overwrite its elements.
In the above example $results will be something along the lines of :
$results[0]=>
array[3]=>
[id]=>2,
[name]two,
[url]=>www.two.com
$results[1]=>
array[3]=>
[id]=>2,
[name]two,
[url]=>www.two.com
This is because $result is a reference and so each time it changes wherever its referenced from
($results[0] and $results[1]) will also change!
So the Solution is to do something like :
<?php
$stmt->bind_result($id,$name,$url);
while($stmt->fetch()){
$results[]=array('id'=>$id,'name'=>$name,'url'=>$url);
}
?>
Hope that helps!