I have this code
$sql = new PDO('mysql:host=localhost;dbname=b','root','root');
$f = $sql->query('select * from user');
$sql->setFetchMode(PDO::FETCH_ASSOC);
while($row = $f->fetch()){
print_r($row);
}
The output is
Array
(
[id] => 1
[name] => turki
[mail] => ablaf
[pass] => 144
)
Array
(
[id] => 2
[name] => wrfwer
[mail] => fwerf
[pass] => werf
)
and that's what I really want. But if I do this
<?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->fetch());
?>
The output is
Array
(
[id] => 1
[name] => turki
[mail] => ablaf
[pass] => 144
)
It has one result but I have two rows in the table; why is that?
Fetch should be used to display next row from database result
To get all rows you should use fetchAll();
Change your example to:
or if you want use PDOStatement::fetch to