Unexpected result using PDO in php-Collection of common programming errors
By default, PDO will grab a numeric AND an associative array when fetching data. To ensure you only get 1 of the 2, you can specify it as the second parameter in your query function by using either PDO::FETCH_ASSOC
or PDO::FETCH_NUMBER
.
Change
foreach($dbc->query('SELECT * FROM `test` ') as $row)
into
// Fetch associative
foreach($dbc->query('SELECT * FROM `test` ', PDO::FETCH_ASSOC) as $row)
or
// Fetch numeric
foreach($dbc->query('SELECT * FROM `test` ', PDO::FETCH_NUMBER) as $row)
[edit]
You can also set the default fetch mode after you created your PDO connection by doing the following:
// Set default fetch mode to PDO::FETCH_ASSOC
$dbc->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
When doing that, doing
foreach($dbc->query('SELECT * FROM `test` ')
will do exactly the same as
foreach($dbc->query('SELECT * FROM `test` ', PDO::FETCH_ASSOC) as $row)
Hope this helped.