-4

I am outputting results from a table with multiple columns. So I used a foreach loop to loop through but it is returning double results. Here is my code:

<?php
while($row=$stmt_header->fetch(PDO::FETCH_BOTH)) {
?>
    <tr class="odd gradeA">
        <form action='semesters.php' method='post'>

        <?php
        foreach($row as $k)
        {
            echo '<td>'. $k . '</td>';
        }
        ?>

        </form>
    </tr>
<?php
    } ?>....

and the output is here... enter image description here

Any help is appreciated

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • You should [edit] this to fix the formatting and always show errors and messages as text in the question. No one wants to go to an image link for that. –  May 11 '18 at 13:01
  • 5
    If you can’t guess, what the “both” in `PDO::FETCH_BOTH` means ... then go read up on it! – CBroe May 11 '18 at 13:02
  • `PDO::FETCH_BOTH` returns both associated and unassociated arrays. Use `PDO::FETCH_ARRAY` or `PDO::FETCH_ASSOC` instead. – aynber May 11 '18 at 13:02
  • The [doc](http://php.net/manual/en/pdostatement.fetch.php) is really simple to read. – Fanie Void May 11 '18 at 13:04

1 Answers1

3

Replace PDO::FETCH_BOTH with PDO::FETCH_ASSOC or PDO::FETCH_NUM: http://php.net/manual/en/pdostatement.fetch.php

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

Derek
  • 1,826
  • 18
  • 25