0

I want to use prepared statement which works fine if it's just one row, but when I want to build a table it only shows the first row. I'm missing something simple somewhere, but I've tried all sorts of things which just broke.

<?php 
        $dbh = dbh_get();
        $sql = 'SELECT * FROM products
            WHERE prod_id >6
            ORDER BY prod_id';
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
        $r = $stmt->fetch();

print '
    <tr>
        <td>' . $r['login_id'] . '</td>
        <td>' . $r['outlet_code'] . '</td>
        <td>' . $r['user_name'] . '</td>
        <td>' . $r['user_role'] . '</td>
        <td>' . $r['last_login'] . '</td>
        <td>' . $r['user_id'] . '</td>
    </tr>' . "\n";
dbh_free($dbh);
?>

Unsure if it's the php or the sql which is the problem

1 Answer 1

1

You need to loop on the results of $stmt->fetch() as it will return one row per call:

while ($r = $stmt->fetch()) {
    print '
    <tr>
        <td>' . $r['login_id'] . '</td>
        <td>' . $r['outlet_code'] . '</td>
        <td>' . $r['user_name'] . '</td>
        <td>' . $r['user_role'] . '</td>
        <td>' . $r['last_login'] . '</td>
        <td>' . $r['user_id'] . '</td>
    </tr>' . "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.