2

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?

1
  • 1
    fetch used to be fetch one row at a time. You may use fetch in loop or user fetchAll like other say to get all rows. Jun 4, 2013 at 11:00

6 Answers 6

5

Fetch should be used to display the next row from the database result.

To get all rows, you should use fetchAll();

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');
    $sth = $sql->query('select * from user');
    while($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
      print_r($row);
    }
?>
3
  • Have we got some bad copy-pasta here? I don't see where you've defined $sth. If there are no variables to bind to the statement, then a prepared statement is unnecessary overhead. Mar 23, 2022 at 22:42
  • not sure what you meant by prepared statements as this answer clearly doesn't mention it but you were right with $sth it was a typo.
    – Robert
    Mar 24, 2022 at 22:40
  • Now, IMO, sth is misleading. The return from the query isn't a "statement handle" -- it is a result set resource/object. sth would be more appropriate when using a prepared statement. Your hyperlinks are pointing to prepared statement documentation, but your snippet is not using a prepared statement. Mar 24, 2022 at 22:41
2

In that case use fetchAll.

$result = $f->fetchAll();
print_r($result);
0

PDOStatement::fetch only retrieves one record from a PDOStatement object (wherever its pointer is) which is why you have to loop through fetch.

0

The fetch() method only returns a single record and sets the internal pointer to point to the next record. You are expecting from this method what it cannot return.

Perhaps try a different method like fetchAll:

<?php
    $sth = $dbh->prepare("SELECT name, colour FROM fruit");
    $sth->execute();

    /* Fetch all of the remaining rows in the result set */
    print("Fetch all of the remaining rows in the result set:\n");
    $result = $sth->fetchAll();
    print_r($result);
?>
0

$f->fetch() moves the internal result pointer to the next value (if it exists). That’s why you get all the values when you call it in while();.

0

$f->fetch() - Will get one row

$f->fetchAll() - Will get all rows

Not the answer you're looking for? Browse other questions tagged or ask your own question.