Fetch should be used to display next row from database result
To get all rows you should use fetchAll();
- PDOStatement::fetch — Fetches the next row from a result set
- PDOStatement::fetchAll() — Returns an array containing all of the result set rows
Change your example to:
<?php
$sql = new PDO('mysql:host=localhost;dbname=b','root','root');
$f = $sql->query('select * from user');
$f->setFetchMode(PDO::FETCH_ASSOC);
print_r($f->fetchAll());
?>
or if you want use PDOStatement::fetch to
<?php
$sql = new PDO('mysql:host=localhost;dbname=b','root','root');
$f = $sql->query('select * from user');
while($row = $sth->fetch(PDO::FETCH_ASSOC))
{
print_r($row);
}
?>