Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.