PDO:: Fetch & Fetchall

Asked

Viewed 20,245 times

3

Because the following code works:

 $stmt = $pdo->prepare("SELECT * FROM dados");
 $stmt->execute();
 $codigos = $stmt->fetch();

 echo $codigos['codigo'];

And so I can’t use?

   $stmt = $pdo->prepare("SELECT * FROM dados");
   $stmt->execute();
   $codigos = $stmt->fetchAll();

   echo $codigos['codigo'];

4 answers

9

Good friend the difference between the fetch and the fetchAll is in return. In the case of fetch is a simple array, while in fetchAll is a multidimensional array, also called a matrix. In your situation the fetch because it only has one record in the bank, from the moment it has more than one record in the bank, it will release a PDOException, as it is receiving more than one object from the bank while only supporting one, unless it uses a LIMIT 0,1 in your query, however this will only bring a result of the bank and do not believe that is what you need. For this you would have to use the fetchAll and to print on the screen would have to use the foreach, that would be the code:

foreach($codigos as $item)
{
   echo $item["codigo"];
}
  • what I do to access positions of this matrix that it returns to me?

  • 1

    With the code described above, making a foreach, you will be able to access each loop of the array.

4

fetch() returns only one row of the array, while fetchAll() returns more than one, i.e., this array is indexed by numbers, it takes a loop to display all information.

To get the numeric indices use the function array_keys:

$codigos = $stmt->fetchAll();
$keys = array_keys($codigos);

The array returned by fetchAll is in this format:

Array
(
[0] => Array
    (
        [nome] => a
        [id] => 1
    )

[1] => Array
    (
        [nome] => b
        [id] => 2
    )

[2] => Array
    (
        [nome] => c
        [id] => 3
    )
  • I cannot 'capture' these indices so that I can store them in arrays separated by the indices I use in select, you have an idea of how to do this?

  • @Thiago Straight I don’t understand, you want to take the numeric key? Pq need to save them in separate arrays?

  • i need to use a table select and migrate this data to other tables, and well I am not able to store to re-use the results obtained, because of this return in fetchAll

  • vc will not need the value of the keys to migrate the data from one table to another. only use foreach to iterate the result, and create one or more Insert in the other table.

  • If you are doing this code only to migrate from one table to another consider doing it only using SQL. Straight into the database.

1

Why the data structure is different from a look at the documentation of the fetch and of fetchAll you will see the difference. Basically get used to using the print_r or the var_dump to see the structure of the data you are working on

-1

Good afternoon Rafael Almeida, was with the doubt similar to yours, my doubt was the opposite of yours, because I was in need of a list/ array of objects, but my code only returned me an object, so I searched in [Pdostatement]: https://www.php.net/manual/en/pdostatement.rowcount.php and saw that the fetch() method returns me the last object so select and fetchAll returns me the full select if there is more than one record in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.