note 99562 added to pdostatement.fetch

From: Date: Tue, 24 Aug 2010 17:50:39 +0000
Subject: note 99562 added to pdostatement.fetch
Groups: php.notes 
Request: Send a blank email to php-notes+get-171994@lists.php.net to get a copy of this message
When fetching multiple rows into an object. It is necessary to clone the objects returned by the PDOStatement::fetch() operation. Otherwise you will just get a list of pointers to the object located inside the statement, containing the information of the last row. Example: <?php $stmt = $pdoLink->prepare("SELECT * FROM table;"); $stmt->setFetchMode(PDO::FETCH_INTO, new myClass()); $stmt->execute(); // Assuming there are multiple rows in the table. while ($object = $stmt->fetch()) { $result[] = clone $object; } var_dump($result); ?> ---- Server IP: 91.199.167.223 Probable Submitter: 80.127.85.60 ---- Manual Page -- http://www.php.net/manual/en/pdostatement.fetch.php Edit -- https://master.php.net/note/edit/99562 Del: integrated -- https://master.php.net/note/delete/99562/integrated Del: useless -- https://master.php.net/note/delete/99562/useless Del: bad code -- https://master.php.net/note/delete/99562/bad+code Del: spam -- https://master.php.net/note/delete/99562/spam Del: non-english -- https://master.php.net/note/delete/99562/non-english Del: in docs -- https://master.php.net/note/delete/99562/in+docs Del: other reasons-- https://master.php.net/note/delete/99562 Reject -- https://master.php.net/note/reject/99562 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171994) next »