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
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".
-
0:00
Now that you have at least a basic understanding of objects, methods,
-
0:05
properties, and classes, you're ready to write code to instantiate or
-
0:09
create an instance of an object, using the PDO class.
-
0:14
We'll then use this object to connect to our database.
-
0:17
Let's go back to our workspace.
-
0:19
Let's create a file we can use to set up the code for connecting to the database.
-
0:24
Let's add this file in the includes directory.
-
0:30
And we'll name it connection.php.
-
0:35
To create an object from a class, you first specify a variable name for
-
0:39
the object you want to create.
-
0:42
Let's open our php, and then create db for database.
-
0:48
We want to assign this variable a value.
-
0:51
So we use the single equal sign.
-
0:55
To create a new object, we use the keyword new.
-
0:59
We then specify the class name, followed by an open parenthesis.
-
1:03
PDO open parenthesis,
-
1:06
creating a new object from a class calls a function or method inside the class.
-
1:11
So use parenthesis after the class name, like you do with functions and
-
1:15
other methods.
-
1:16
Just like with functions,
-
1:18
when you create a new object from a class, you can often pass in arguments.
-
1:23
With the PDO class,
-
1:24
you pass in the information needed to connect to the database.
-
1:28
We'll be using SQLight for the following example.
-
1:30
So let's see what's needed for that database.
-
1:34
We'll do that by opening a new tab in our browser.
-
1:38
We'll do a search for pdo drivers.
-
1:43
A driver is a piece of software that connects one thing to another.
-
1:47
In this case, we're looking for
-
1:48
the PDO driver that connects SQLight to PHP using PDO.
-
1:54
The first result is the one we want.
-
1:57
You can see here that we have several different PDO drivers.
-
2:01
We're going to want SQLite.
-
2:03
So let's click on SQLLite And let's see what that requires.
-
2:07
It says that PDO_SQLITE is a driver that implements the PHP
-
2:11
data objects interface to enable access to SQLite three databases.
-
2:17
In order to do that, we're going to need a certain string.
-
2:20
We need this SQLITE DSN.
-
2:24
So we will need the data source name,
-
2:27
which is SQLite, to access a database on disk, which is what we have.
-
2:32
We can see that we need to append the absolute path to the DSN prefix.
-
2:38
So we need SQLite, followed by the path to the database.
-
2:42
Okay, let's go make this work.
-
2:44
Within our call to the PDO class, we're going to add SQLite within quotes,
-
2:51
followed by a colon and the path to our database.
-
2:56
Instead of the full path, we're going to use a special magic constant that php has.
-
3:01
_ _DIR_ _, this means to look in the exact
-
3:07
directory where this file is located.
-
3:14
We're then going to use /database.db,
-
3:18
close the parentheses and end the line.
-
3:23
If you wanna learn more about magic constants, check out the teacher's notes.
-
3:28
We're gonna save this file.
-
3:30
And just to make sure that we've created our new object,
-
3:32
we're going to do a var_dump.
-
3:37
We're gonna dump the actual database object we created.
-
3:40
We'll save this and preview in the browser again.
-
3:43
We're going to have to type in inc/connection.php.
-
3:51
Okay so here we see that it's dumping an object, it's a PDO object.
-
3:56
The PDO object is connecting to our database but
-
3:59
sometimes, you might have a typo in your code or, for some other reason,
-
4:04
PHP may not be able to connect to your database.
-
4:07
In the next video, I'll show you how to tackle unexpected problems and
-
4:11
handle errors gracefully.
You need to sign up for Treehouse in order to download course files.
Sign up