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
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM mytable');
foreach ($stmt as $row) {
print_r($row);
}
$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.