Crud making a mistake on the line where the foreach is?

Asked

Viewed 69 times

0

I’m studying creation of Crud with Php and Bootstrap, and when the file menu_admin and opened in the browser it gives this error:

Warning : Invalid argument supplied for foreach() in C: xampp htdocs crud menu_admin.php on line 44

Link to github, who can help me thank you very much !!!

https://github.com/rockout13/crud

I changed what they said, but another problem appeared, it would be the query that is failing ?

Fatal error : Uncaught Error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\crud\menu_admin.php:45 Stack trace: #0 {main} thrown in C:\xampp\htdocs\crud\menu_admin.php on line 45

1 answer

1


The method query of PDO returns nothing see here, he just executes the query. What will return you the lines that came from the database in one array are the methods fetch, in the example below I will do with the fetchAll:

include 'banco.php';
$pdo = Banco::conectar();
$sql = 'SELECT * FROM pessoa ORDER BY id DESC';

foreach($pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $row)
{
    echo '<tr>';
    echo '<td>'. $row['nome'] . '</td>';
    echo '<td>'. $row['CPF'] . '</td>';
    echo '<td>'. $row['Endereço'] . '</td>';
    echo '<td>'. $row['email'] . '</td>';
    echo '<td width=250>';
    echo '<a class="btn btn-primary" href="read.php?id='.$row['id'].'">Listar</a>';
    echo ' ';
    echo '<a class="btn btn-warning" href="update.php?id='.$row['id'].'">Atualizar</a>';
    echo ' ';
    echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Excluir</a>';
    echo '</td>';
    echo '<tr>';
}
  • 1

    Just put "->" in the foreach($Pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $Row){

  • Vlw, I ended up mixing other languages kkk

  • Edited response

  • i changed what they said, but another problem appeared, is the query that is failing ? Fatal error&#xA;: Uncaught Error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\crud\menu_admin.php:45 Stack trace: #0 {main} thrown in&#xA;C:\xampp\htdocs\crud\menu_admin.php&#xA;on line&#xA;45

  • Probably because when the query of error return false, as false is the type boolean, does not have the function fetchAll. Try to run the query direct at the bank.

Browser other questions tagged

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