"fetch in pdo gets only one result [duplicate]" Answer’s

0

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);
 }
 ?>
Wednesday, March 31, 2021
 
eek
answered 11 Months ago
eek
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :