Crud is giving error in the line where the forEach has?

0

I am studying Crud creation with Php and Bootstrap , and when the menu_admin file is opened in the browser he gives this error:

  

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

github link, who can help me thank you very much !!!

link

I changed what they said, but another problem appeared, would the query be 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

    
asked by anonymous 24.01.2018 / 16:51

1 answer

1

The method query of PDO returns nothing see here , it just runs the query . What will return the rows that came from the database in array are fetch methods, in the example below I will do with 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>';
}
    
24.01.2018 / 17:00