Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video

- 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
The PHP Data Object (PDO) allows PHP to interact with a database. This project uses a SQLite database, but you can connect to many different types of databases using a PDO. This video will show you how to find and use the documentation for PDO so you can then use that documentation to connect to other databases as well.
Magic Constants
PHP provides a large number of predefined constants to any script which it runs. More information on these Magic Constants can be found here.
Connecting PDO to MySQL
$db = new PDO("mysql:host=localhost;dbname=DATABASE_NAME;port=3306","USERNAME","PASSWORD");
For MySQL, you specify mysql and a colon, then type "host=" followed by the name of the host to which you want to connect. After that, you type a semicolon, followed by dbname and an equal sign, followed by the name of the database. If your environment is using the default MySQL port of 3306, this string here might be all you need.
MAMP uses a different port (8889 by default), so if you haven't changed the default, you will have to specify the port number. You type another semicolon, followed by the word "port" and an equal sign, followed by the port number.
That's the first argument to pass in when instantiating a new object from the PDO class. It's all surrounded by 1 set of quotation marks because it's 1 PHP string.
The word “host”, here, is not a PHP variable— it's just part of a piece of text that gets used to identify the database.
For MySQL, the second argument is the username and the third argument is the password. By default, the MySQL username and password on MAMP are both "root".
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Siraj Khan
3,451 Points1 Answer
-
jlampstack
23,932 Points1 Answer
-
jlampstack
23,932 Points3 Answers
-
Dennis Amiel Domingo
17,813 Points3 Answers
-
Kristofer Doman
18,307 Points0 Answers
-
jlampstack
23,932 Points0 Answers
-
Alex Flores
7,864 Points2 Answers
-
Dawn tang
2,862 Points4 Answers
-
Stephen Limmex
32,695 Points2 Answers
-
WK h
2,924 Points0 Answers
-
ammarkhan
Front End Web Development Techdegree Student 21,661 Points0 Answers
-
George Michael
1,783 Points2 Answers
-
Javier MARQUEZ
11,877 Points6 Answers
-
Adam Siwiec
12,070 PointsHow do I connect to a remote database (not localhost)
Posted by Adam SiwiecAdam Siwiec
12,070 Points1 Answer
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