Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Integrating PHP with Databases!
You have completed Integrating PHP with Databases!
Preview
In this video, we’ll use our PDO Statement Object to return all the media items from the database in an associative array. This will allow us to use that data to display the media items on our site.
Links
Example Code
$results = $db->query("SELECT title, category FROM Media");
var_dump( $results->fetchAll(PDO::FETCH_ASSOC) );
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
This loads all of our media items from
the database into an array variable.
0:00
That array resembles our catalog array.
0:00
Let's take a look at the documentation for
fetchAll and
0:03
modify our query to more closely
match our catalog array.
0:07
In a browser, when we view
the page source on connection.php,
0:11
we can see that the fetchAll method
returns a multi-dimensional array,
0:15
with one element for each item.
0:19
Each item then contains a secondary
array with one element for
0:22
each of the item attributes, you'll notice
that each piece of data shows up twice.
0:26
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