Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Using PHP with MySQL Querying the Database with PHP Retrieving the Result Set

James Barrett
James Barrett
13,253 Points

Why does the array return the same value twice?

array(1) { [0]=> array(16) { ["U_ID"]=> string(1) "1" [0]=> string(1) "1" ["U_Forename"]=> string(5) "James" [1]=> string(5) "James" ["U_Surname"]=> string(7) "Barrett" [2]=> string(7) "Barrett" ["U_Email"]=> string(25) "james-barrett@hotmail.com" [3]=> string(25) "james-barrett@hotmail.com" ["U_Password"]=> string(8) "password" [4]=> string(8) "password" ["U_Team"]=> string(7) "Arsenal" [5]=> string(7) "Arsenal" ["U_Biography"]=> string(19) "Hi there I am James" [6]=> string(19) "Hi there I am James" ["U_Activated"]=> string(1) "1" [7]=> string(1) "1" } }

I am working on my own project and the fetchAll() returns an array of the result. However why is it two-dimensional and why is it returning the same value twice?

Thanks, James.

James Barrett
James Barrett
13,253 Points

Apologies for formatting of the code... I did indent it however it did not do it correctly :S

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

Because fetchAll() returns both the associated array (column names as key) as well as the numerical array. You can set the fetch mode as an argument: PDO fetchAll

Or you can use the regular fetch() witch returns one row at a time, and loop it to get all the rows from your query.