note 130242 added to pdostatement.getiterator

From: Date: Thu, 10 Apr 2025 14:56:04 +0000
Subject: note 130242 added to pdostatement.getiterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-222080@lists.php.net to get a copy of this message
One detail to add to what berxudar said. Because this statement comes from the IteratorAggregate interface, it is recognized by foreach() directly, meaning there's no need to collect the iterator just to make a foreach(). Editing the code according to this : <?php // Establish a database connection $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password'); // Prepare and execute an SQL query $stmt = $pdo->query('SELECT * FROM mytable'); // Process the result set using a loop foreach ($stmt as $row) { // $row represents a row of the result set print_r($row); } // Close the PDOStatement and the connection $stmt = null; $pdo = null; ?> Note we gave $stmt to foreach() directly. Foreach() will see that $stmt is a PDOStatement object and as such is an object of IteratorAggregate, and will call on its own this method to get an iterator to work with. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 2a02:8428:bf12:f301:3dc:4234:75ec:7e10) ---- Manual Page -- https://php.net/manual/en/pdostatement.getiterator.php Edit -- https://main.php.net/note/edit/130242 Del: integrated -- https://main.php.net/note/delete/130242/integrated Del: useless -- https://main.php.net/note/delete/130242/useless Del: bad code -- https://main.php.net/note/delete/130242/bad+code Del: spam -- https://main.php.net/note/delete/130242/spam Del: non-english -- https://main.php.net/note/delete/130242/non-english Del: in docs -- https://main.php.net/note/delete/130242/in+docs Del: other reasons-- https://main.php.net/note/delete/130242 Reject -- https://main.php.net/note/reject/130242 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222080) next »