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