Re: foreach

From: Date: Wed, 17 Oct 2012 02:10:01 +0000
Subject: Re: foreach
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-319496@lists.php.net to get a copy of this message
On Tue, Oct 16, 2012 at 6:25 PM, Larry Garfield <larry@garfieldtech.com> wrote: > Instead, use PDO, and bind your parameters. As a nice bonus, the result > from a PDO-based query is not a raw resource but an iteratable object, which > means you can foreach() it. > > http://php.net/manual/en/book.pdo.php > > $conn = new PDO(...); > $result = $conn->query("SELECT * FROM items"); > foreach ($result as $record) { > // Do something with each record here. > } > > --Larry Garfield Bonus round: Remember if you do multiple queries to also issue the close when you are done with the current result set: $result->closeCursor(); (not all dbms drivers have this limitation, but defensive coding is defensive.)

« previous php.general (#319496) next »