Error while listing table record using PHP PDO

Asked

Viewed 30 times

1

Person, I’m trying to list the database table records, but I’m not getting, I’m getting the following error: Undefined index: name in... Undefined index: email in... With all the fields it’s happening, and I believe it’s happening because I’m using the fetchAll() from PDO, because when I only use the fetch(), the script runs normally, however, displays only one record, I count on you to assist me in this...

Source:

<?php

include_once 'header.php';
require_once 'bitch.php'; //Connection with database
?>

<div class="row">
    <div class="col s12 m6 push-m3">
       <h3>Clients</h3>
        <table class="striped">
                <thead>
                    <th>Nome:</th>
                    <th>Sobrenome:</th>
                    <th>Email:</th>
                    <th>Idade:</th>
                </thead>
                <tbody>
                    <?php
                        $sql = "SELECT * FROM crud";
                        $result = $conn->query($sql);
                        $result->execute();
                        if($result->rowCount() > 0):
                            $row = $result->fetchAll();
                            //print_r($row);
                    ?>
                    <td><?php echo $row['nome']; ?></td>
                    <td><?php echo $row['sobrenome']; ?></td>
                    <td><?php echo $row['email']; ?></td>
                    <td><?php echo $row['idade']; ?></td>
                    <td><a href="#" class="btn-floating orange"><i class="material-icons">edit</i></a></td>
                    <td><a href="#" class="btn-floating red"><i class="material-icons">delete</i></a></td>
                    <?php endif ?>
                </tbody>
        </table>
        <br>
                <a href="adicionar.php" class="btn">ADICIONAR</a>
    </div>
</div>



<?php

include_once 'footer.php';

?>

1 answer

1


The method fetch returns a result, the method fetchAll returns all. Therefore you should iterate on the result of fetchAll:

<?php
$sql = "SELECT * FROM crud";
$result = $conn->query($sql);
$result->execute();
if($result->rowCount() > 0):
  $rows = $result->fetchAll();
  foreach($rows as $row):
?>
                  <tr>
                    <td><?php echo $row['nome']; ?></td>
                    <td><?php echo $row['sobrenome']; ?></td>
                    <td><?php echo $row['email']; ?></td>
                    <td><?php echo $row['idade']; ?></td>
                    <td><a href="#" class="btn-floating orange"><i class="material-icons">edit</i></a></td>
                    <td><a href="#" class="btn-floating red"><i class="material-icons">delete</i></a></td>
                  </tr>
<?php
  endforeach;
endif;
?>
  • Thanks, it worked. However, there is still a mistake, can you help me? It is that the records are being displayed without breaking line, all of them are in a single line

  • True, the table rows are missing. The table rows are represented by the tag <tr>. I edited the answer with tr.

  • Okay, now it’s worked out, thank you so much for everything!

  • I’m glad you liked the answers! But the best way to thank those who helped you is to mark "accept" the best answer and vote for all who helped you. This way you make sure that whoever wrote the answer gets something in return, as well as making the site cleaner and more useful for everyone. Adding a new answer like this (which is not an answer to the question and should be removed) makes the site more confusing and can get in the way.

Browser other questions tagged

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