note 100945 added to mysqli-stmt.fetch

From: Date: Tue, 16 Nov 2010 16:05:36 +0000
Subject: note 100945 added to mysqli-stmt.fetch
Groups: php.notes 
Request: Send a blank email to php-notes+get-174088@lists.php.net to get a copy of this message
"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! ---- Server IP: 69.147.83.197 Probable Submitter: 86.23.76.65 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.fetch.php Edit -- https://master.php.net/note/edit/100945 Del: integrated -- https://master.php.net/note/delete/100945/integrated Del: useless -- https://master.php.net/note/delete/100945/useless Del: bad code -- https://master.php.net/note/delete/100945/bad+code Del: spam -- https://master.php.net/note/delete/100945/spam Del: non-english -- https://master.php.net/note/delete/100945/non-english Del: in docs -- https://master.php.net/note/delete/100945/in+docs Del: other reasons-- https://master.php.net/note/delete/100945 Reject -- https://master.php.net/note/reject/100945 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174088) next »