I'm a php and mysql beginner, I'm currently self study PDO and confused some concepts:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$sql = "SELECT * FROM users";
$users = $dbh->query($sql);
1.What is the relationship between PDO class and PDOStatement class?
$dbh
is the new object of class PDO, but why $users is the PDOStatement object? fetchAll()
is the function inside class PDOStatement
, but you can use it like this $users->fetchAll()
, is $users
a PDO
or PDOStatement
object?
2.Someone said $users is the cursor, once consumed, it won't rewind to the beginning of the resultset.
foreach ($users as $row) {
print $row["name"] . "<br/>";
}
but why you can use it in a foreach
statement? foreach
provides a way to iterate over arrays. what is cursor actually? is cursor a pointer?
3.For the pdostatement class, the doc said:
PDOStatement implements Traversable { ... }
why this class implements Traversable interface? is it empty interface?
Thank you for help!