Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Building a php-driven page is just the first step in mastering PHP. The true power of PHP shines when you connect it to a data source like an API or a Database. In this course, we’ll refactor an application to use a database to store the information. This will make our application easier and faster to scale. It will also make it easier and faster to add functionality like the search and pagination we’ll also be adding in this project.
Learn More About Relational Databases
https://teamtreehouse.com/library/database-foundations
https://teamtreehouse.com/library/sql-basics
Database Diagram
Using the Database Locally
Included in the download is a .sql file for importing the data into another database such as MySQL. Once you have this file, you can choose one of the following options for loading the database into your local dev environment
CAREFUL: These steps will replace all tables in any existing database named "database"!
1. Import Database into MySQL using phpMyAdmin
If you are using MAMP, you can find phpMyAdmin by starting your servers and going to http://localhost/phpMyAdmin or http://localhost:8888/phpMyAdmin
Then choose the import tab and select the downloaded database.sql file.
2. Importing Database into MySQL using Command Line
Step One:
Open a new terminal (Mac) or command line (Windows) window
Step Two:
[PATH_TO_MYSQL] -u [USERNAME] -p < [PATH_TO_SQL_FILE] Hit the Enter Key
Example Mac with MAMP: /applications/MAMP/library/bin/mysql -u root -p < /Downloads/database.sql
Example Windows with MAMP: c:/MAMP/bin/mysql/bin/mysql -u root -p < c:/Downloads/database.sql
Quick Tip: Don’t forget that you can simply drag the file into the terminal window and it will enter the location of the file for you.
Step Three:
You should be prompted with the following line:
Enter password:
Type your password, keep in mind that the letters will not appear, but they are there Hit the enter key.
Quick Tip: By default, the MySQL password for MAMP is "root".
Step Four:
Check if you database was successfully imported Navigate to phpMyAdmin in a browser
http://localhost/phpMyAdmin OR http://localhost:8888/phpMyAdmin
-
0:01
Hi I'm Alena, a PHP teacher at Treehouse.
-
0:08
I'm going to be teaching you how to connect a basic PHP website to a database.
-
0:12
This will allow you to dynamically update your web pages
-
0:15
with constantly changing information.
-
0:18
You already know the basics of PHP but
-
0:21
building a PHP driven page is just the first step in mastering PHP.
-
0:27
The true power of PHP shines when you connect it to a data source,
-
0:31
like an API or a database.
-
0:33
To introduce you to databases, we'll start with an example project,
-
0:37
a personal media library.
-
0:39
Currently the application is pretty simple and
-
0:42
it stores its data in an associative array.
-
0:45
An associative array can become extremely hard to maintain.
-
0:49
Since it's just text, we could easily miss adding a piece of information.
-
0:54
We could also mistype the name of a field,
-
0:56
which would stop our site from using it at all.
-
1:00
We could handle these issues, by writing our own interface with PHP.
-
1:05
However, one of the first things you should learn about programming is,
-
1:09
don't reinvent the wheel.
-
1:11
Use the right tool for the job, a database is designed to store data.
-
1:16
If you need to store data, like our media items, a database is a good choice.
-
1:22
Another important thing to consider,
-
1:24
anytime we want to use the data that stored in an array.
-
1:28
We need to include the entire array, even if we only need one item.
-
1:33
With twelve items, this is not really an issue.
-
1:35
But with 1,200 items, things can really start to slow down.
-
1:40
A database can also give us more advanced sorting and search capabilities.
-
1:46
We'll refactor that application to use a database to store the media items.
-
1:51
This will make our application easier and faster to scale.
-
1:54
It also makes it easier and faster to add functionality.
-
1:58
We'll be adding search and pagination functionality to this project.
-
2:02
Using this library data,
-
2:04
we'll learn how to communicate using php's data objects or pto for short.
-
2:10
Pto uses object oriented methodology,
-
2:13
which makes interacting with databases very efficient.
-
2:16
We'll be covering everything you need to know, so
-
2:18
don't be afraid if you don't understand object oriented programming.
-
2:23
We'll be using relational databases for
-
2:25
this project because it fits well with the way our data relates to each other.
-
2:30
To learn more about relational databases,
-
2:32
make sure you check the teacher's notes for more great courses.
-
2:36
For now, let's take a look at the way our data is structured.
-
2:40
The table structure that we'll be working with has a media table, a genre table and
-
2:44
a people table.
-
2:45
There is only one genre assigned to each item, so
-
2:48
we can link directly from the media table to the genre's table.
-
2:52
On the other hand, there are multiple people assigned to each item.
-
2:56
So, we link the media and the people table together, using another table.
-
3:01
That table also tells us which role that person performs.
-
3:05
For example, the movie Office Space has a director, Mike Judge and
-
3:10
stars Ron Livingston, Jennifer Aniston, and David Herman.
-
3:16
This simple structure will allow you to practice with relational data,
-
3:20
as you learn your way around a database.
-
3:22
For reference, I've also included a chart in the teacher's notes
-
3:26
that describes the database structure.
-
3:28
Our main focus will be in the media table and
-
3:31
we'll be pulling supporting information from the other tables.
-
3:34
Let's take a look at our project.
-
3:37
If you open the workspace attached to this video, you'll see all the files for
-
3:40
our personal media library.
-
3:41
You'll also see a database file within the includes folder, named database.db.
-
3:48
Our database file is local to the workspace, so
-
3:51
it's really great for you to be able to play with.
-
3:54
Don't be afraid to experiment and try out different things with the database.
-
3:57
You can always replace the file with a fresh copy.
-
4:01
In workspaces we'll be using SQlite.
-
4:04
However, most of the query commands will be the same,
-
4:07
no matter which relational database you use.
-
4:10
Where SQlite differs, I'll highlight those differences and
-
4:12
add them to the teacher's notes.
-
4:14
You're also welcome to download the project files and install them locally.
-
4:18
Within the download, I've also included a dot sequel file for
-
4:21
importing the data into another database, such as my sequel.
-
4:25
Check the teachers notes, if you need more instruction.
-
4:28
When we go over and
-
4:29
hit preview, we can see how the application works in the browser.
-
4:33
On the home page,
-
4:34
we are showing four random media items, each time the page is loaded.
-
4:38
We also have the links at the top to view the library categories, books,
-
4:44
movies and music.
-
4:46
From the category pages we can also link to the full catalog,
-
4:50
there is also a link to this suggest page.
-
4:52
Which includes a form for sending a suggestion for a new media item.
-
4:56
After connecting to the database, we'll add a couple new features.
-
5:00
So, let's take a look at the finished site as well.
-
5:02
First of all we'll be adding a search feature.
-
5:05
Can you imagine browsing the internet without the ability to search?
-
5:09
The internet is full of information on almost anything you can think of.
-
5:12
But how would we possibly find it, without the ability to search?
-
5:17
In this course we'll be introducing you to some of the basic components of search.
-
5:21
By adding the ability to search the media library.
-
5:24
Another useful item as our catalog grows is pagination.
-
5:28
Listing a million items on a single page would make for a really slow loading page.
-
5:33
Can you imagine trying to shop on Amazon if every single item was on one page?
-
5:38
Pagination allows us to split the items across multiple pages and
-
5:42
navigate between those pages.
-
5:44
It's kind of like the old adage, how do you eat an elephant, one bite at a time.
-
5:50
Pagination allows us to give our users the data in bite sized chunks.
-
5:55
Finally, we'll use our database to simplify our form drop downs as well.
-
6:00
All this code- Will be simplified into this function.
-
6:07
Currently all the data about the items used in our application
-
6:11
is stored in an array.
-
6:13
Moving the data to a database will give us more flexibility for organization.
-
6:18
And allows us to select only part of the data
-
6:21
without having to loop through each item in the array.
-
6:24
This becomes even more important as our data set grows.
-
6:28
Our first step is to look at the php data object
-
6:31
to set up the connection to the database.
-
6:33
So let's get started
You need to sign up for Treehouse in order to download course files.
Sign up