Re: foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Oct 16, 2012 at 6:25 PM, Larry Garfield <larry@xxxxxxxxxxxxxxxx> 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.)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux