0

i am getting the "$field_value" twice - but why? normal the structure is: <id><name><address><email> but now i get <id><id><name><name><address><address><email><email>

here is the code:

 <tbody>

        <?php
        $STH = $DBH->prepare("SELECT * FROM kunden");
        $STH->execute();
        $result = $STH->fetchall();

        foreach($result as $key => $inner_arr) {
            echo '<tr>';
            foreach($inner_arr as $field_name => $field_value) {
                echo "<td>{$field_value}</td>";
            }
            echo '</tr>';
        }

        ?>

</tbody>
Mike
  • 163
  • 1
  • 4
  • 19
  • you have `for each Value of Result and for each Value of InnerArray => print smth` so for each both of them, you have twice more iterations – Royal Bg Aug 28 '13 at 09:37

1 Answers1

6

You need to set the fetchMode:

Default is PDO::FETCH_BOTH, so an array indexed by both column name and number will be returned.

$result = $STH->fetchall(PDO::FETCH_ASSOC);
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 1
    [Or set default fetch mode on connection.](http://stackoverflow.com/questions/3893858/is-is-possible-to-set-a-default-pdo-fetch-mode) – Glavić Aug 28 '13 at 09:43