1

I'm using PDO and I'm trying to print the results on a table, but the values don't appear. I need to use the <td> tags.

Here is my complete code with a help of francisco:

 <?php
    require_once('config.php');


    $stmt = $connect->prepare("SELECT id, username, email FROM user");
    $stmt->execute();  
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    ?>  


                <table id="table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Username</th>
                            <th>email</th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                       foreach ($result as $row): 

                            ?>
                            <tr>
                                <td><?= $row['id'];?></td>
                                <td><?= $row['username'];?></td>
                                <td><?= $row['email'];?></td>

                            </tr>
                            <?php
                        endforeach;
                        ?>
                    </tbody>
                </table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    What is your resulting html? – Francisco Presencia Oct 03 '13 at 10:51
  • I'll dig into it, so far, it's `fetchAll()` instead of `fetch()` if you want to get every user. Do you have [error handling set to strict and logging them](http://stackoverflow.com/q/1248952/938236)? Do that before even including the config.php and see what the error says. – Francisco Presencia Oct 03 '13 at 12:16

1 Answers1

0

You need to call fetchAll() outside of loop OR call fetch() in each iteration like this:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    // do sth

If you want foreach, just call fetchAll() and do foreach loop over result.

aso
  • 1,331
  • 4
  • 14
  • 29