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
A PHP Data object or PDO is an extension that allows us to have a consistent way of interfacing with databases making our communication much easier from within our project. The first step in working with our data is to use a database specific PDO Driver.
If you are unable to trigger an Exception by just misspelling the database name, try changing it to look in a different location.
Such as:
$db = new PDO('sqlite:/database.db');
Notice the / instead of the ./ which is a very common error.
-
0:00
A PHP data object, or
-
0:02
PDO, is an extension that allows us to have a consistent way of interfacing with
-
0:06
databases, making our communication much easier from within our project.
-
0:10
The first step in working with our data is to use a database-specific PDO driver.
-
0:16
Before we create our PDO and
-
0:18
connect to our database, we should do a quick review of some object-oriented PHP.
-
0:24
Since our PDO and all that it returns is in OOP form, our object or
-
0:29
our PDO PHP data object is an instant of the PDO class.
-
0:35
That object it will have its own variables called properties, and
-
0:39
its own built in functions or methods.
-
0:42
To start with,
-
0:43
we're gonna need to connect to our database using a PDO database driver.
-
0:48
We'll be using SQLite for
-
0:49
the following example, so let's see what's needed for that database.
-
0:53
So we'll do that by opening up a tab, and doing a search for PDO drivers.
-
0:58
[SOUND] Okay, and the first one is the one we want.
-
1:05
And then we can see here we have several different PDO drivers.
-
1:09
You'll see it says database extensions here at the top,
-
1:12
abstraction layers and PDO.
-
1:15
If you end up on just the PDO main page, you can scroll down, and you'll see that
-
1:19
the very bottom, or pretty close to the bottom here, you'll see PDO drivers.
-
1:25
So that's where we left off.
-
1:27
So in PDO drivers, we're gonna want SQLite, so we click SQLite.
-
1:31
Let's see what it requires.
-
1:33
Says PDO SQLite driver that implements the PHP data objects interface,
-
1:38
which we'll be working with, to enable access to SQLite 3 databases.
-
1:42
In order to do that, we're going to need a certain string.
-
1:46
Let's see here.
-
1:49
We need a PDO DSN, okay?
-
1:51
So we will need the data source name, or
-
1:55
DSN, which is the prefix, which is going to be SQLite.
-
1:59
And we can see more info here, and to access a database on disc,
-
2:04
which is what we have, we would append the absolute path to the DSN prefix.
-
2:10
Okay, so taking all that in, let's go make it work.
-
2:14
Head back over to your workspaces.
-
2:15
You can leave this open.
-
2:16
I'm gonna go ahead and close it.
-
2:18
And now, I'm gonna go ahead and bring my console down.
-
2:21
If it's still there, you can just tuck it away.
-
2:23
Or if you want, you can do Hide Console.
-
2:27
I'm gonna open up my index.php file, scroll to the very top, and
-
2:32
here I'm gonna open up some standard PHP tags, and
-
2:34
we'll add our connection code to start with, here in the index file.
-
2:43
Go ahead and close it.
-
2:46
And inside here, we're gonna wanna do a couple of things.
-
2:51
The very first thing that we're gonna wanna do,
-
2:54
is to enable our error reporting.
-
2:58
And to do that, we're going to first add a comment, and that comment is
-
3:02
gonna remind us to get rid of this code before we put it in production, because we
-
3:06
don't wanna display errors to just any user, it's just for development purposes.
-
3:12
So I'm gonna say remove before flight.
-
3:18
Okay, in the next line of code, we're going to call a built-in PHP
-
3:22
function called ini_set, so ini_set.
-
3:27
And then in here, we're gonna have two different strings.
-
3:31
The first string is what we wanna set, which is display_errors.
-
3:38
And that is display_errors.
-
3:41
And then we're gonna put another string, which is what we wanted on or off.
-
3:45
And in our case, we want it on.
-
3:47
Close our line with a semicolon and that's our very first step that we
-
3:51
have error reporting or displaying errors to our screen on.
-
3:55
The next thing we're gonna do is create our PDO object.
-
3:59
We do that through object oriented code by instantiating a new PDO object.
-
4:05
So, first we give it a variable name to store the object in, so
-
4:10
$ and I'll go with DB for database.
-
4:14
The next is the keyword new to create a new object.
-
4:18
And then our class, which is capital P-D-O, and then open and
-
4:22
close parenthesis and close our line.
-
4:25
So inside of our PDO, the first thing that happens is we need a DSN.
-
4:31
And for us, if we recall, it was SQLite,
-
4:34
followed by a colon, followed by the absolute path of the database on disk.
-
4:39
So, head back a couple, open up some strings, and then we're gonna do sqlite:,
-
4:47
in our case, it's in the same folder, so
-
4:50
we're gonna do ./ and then database.db.
-
4:58
Okay. We're gonna save that,
-
5:00
go down to the next line, and just to see that we've created our new object,
-
5:05
we're gonna do a var dump for the actual DB object that we created.
-
5:11
So let's do that now.
-
5:12
Var_dump, open and close our parentheses,
-
5:19
and then inside our object $db, close our line with a semi colon, hit Save.
-
5:25
I'm gonna go ahead and hit die after this.
-
5:30
That will stop the actual rest of the file from loading, hit Save and
-
5:34
then click on our Preview icon.
-
5:37
Okay, so here we are seeing it's dumping a object.
-
5:41
It is a PDO object.
-
5:43
If we were maybe to, perhaps, make a mistake, maybe a spelling error.
-
5:48
So let's add a couple extra's As in here.
-
5:51
Save our file, and then re-hit the icon.
-
5:55
You might get this, which is a fatal error, uncaught exception, and
-
6:00
then PDO exception.
-
6:01
Well, we don't want uncaught exceptions, we don't want that accidentally happening,
-
6:05
so the next thing for us to do is a little bit of error handling.
-
6:09
But before we do the error handling, let me go ahead and fix my error.
-
6:14
So I'm gonna go in close this and remove some of these As.
-
6:19
Save and then re-preview.
-
6:22
All right, great.
-
6:24
Now let's move on to some air handling.
You need to sign up for Treehouse in order to download course files.
Sign up