Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Understanding Relationship Tables 3:41
- Relational Data 5 questions
- Querying Multiple Tables with JOIN 4:02
- JOINing Tables 1 objective
- Understanding SQL Injections 5:21
- SQL Injection 1 question
- Preparing SQL Statements 6:25
- Using a Prepared Statement 3 objectives
- Adding a Secondary Query 4:30
- Fetching in a While Loop 7:38
- Fetching Many Relationships 1 objective

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We have our people and roles in the results object and we need to add them to the item variable so that we can return them back to our function call. We could use the fetchAll method here to retrieve all the authors, but they wouldn't be in the exact format we need. Because each author is a row in the database, fetchAll would give us a whole array for each author, instead of just a simple variable. If you want the results to be anything besides a simple array, you'll need to use a while loop to go through the results one at a time and format them as you go.
Example Code
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item[$row["role"]][] = $row["fullname"];
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Matt Nickolls
9,895 Points1 Answer
-
Matt Nickolls
9,895 Points1 Answer
-
jlampstack
23,932 Points0 Answers
-
Dion Dermott
15,875 Points0 Answers
-
Michael Whalen
4,282 Points1 Answer
-
PLUS
Vladislav Mavrin
Courses Plus Student 3,125 Points1 Answer
-
Jeff Styles
2,784 Points1 Answer
-
Taraj Shah
21,073 Points3 Answers
-
Dhruv Kapoor
2,902 Points2 Answers
-
Mouneeb Shahid
6,176 Points0 Answers
-
PLUS
Ashish Mehra
Courses Plus Student 340 Points4 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up