1 00:00:00,640 --> 00:00:02,130 A PHP data object, or 2 00:00:02,130 --> 00:00:06,000 PDO, is an extension that allows us to have a consistent way of interfacing with 3 00:00:06,000 --> 00:00:10,540 databases, making our communication much easier from within our project. 4 00:00:10,540 --> 00:00:16,010 The first step in working with our data is to use a database-specific PDO driver. 5 00:00:16,010 --> 00:00:18,590 Before we create our PDO and 6 00:00:18,590 --> 00:00:24,040 connect to our database, we should do a quick review of some object-oriented PHP. 7 00:00:24,040 --> 00:00:29,610 Since our PDO and all that it returns is in OOP form, our object or 8 00:00:29,610 --> 00:00:35,430 our PDO PHP data object is an instance of the PDO class. 9 00:00:35,430 --> 00:00:39,385 That object it will have its own variables called properties, and 10 00:00:39,385 --> 00:00:42,670 its own built in functions or methods. 11 00:00:42,670 --> 00:00:43,350 To start with, 12 00:00:43,350 --> 00:00:48,140 we're gonna need to connect to our database using a PDO database driver. 13 00:00:48,140 --> 00:00:49,770 We'll be using SQLite for 14 00:00:49,770 --> 00:00:53,310 the following example, so let's see what's needed for that database. 15 00:00:53,310 --> 00:00:58,075 So we'll do that by opening up a tab, and doing a search for PDO drivers. 16 00:00:58,075 --> 00:01:05,302 [SOUND] Okay, and the first one is the one we want. 17 00:01:05,302 --> 00:01:09,490 And then we can see here we have several different PDO drivers. 18 00:01:09,490 --> 00:01:12,490 You'll see it says database extensions here at the top, 19 00:01:12,490 --> 00:01:15,130 abstraction layers and PDO. 20 00:01:15,130 --> 00:01:19,930 If you end up on just the PDO main page, you can scroll down, and you'll see that 21 00:01:19,930 --> 00:01:25,780 the very bottom, or pretty close to the bottom here, you'll see PDO drivers. 22 00:01:25,780 --> 00:01:27,530 So that's where we left off. 23 00:01:27,530 --> 00:01:31,660 So in PDO drivers, we're gonna want SQLite, so we click SQLite. 24 00:01:31,660 --> 00:01:33,480 Let's see what it requires. 25 00:01:33,480 --> 00:01:38,140 Says PDO SQLite driver that implements the PHP data objects interface, 26 00:01:38,140 --> 00:01:42,630 which we'll be working with, to enable access to SQLite 3 databases. 27 00:01:42,630 --> 00:01:46,970 In order to do that, we're going to need a certain string. 28 00:01:46,970 --> 00:01:47,780 Let's see here. 29 00:01:49,030 --> 00:01:51,710 We need a PDO DSN, okay? 30 00:01:51,710 --> 00:01:55,310 So we will need the data source name, or 31 00:01:55,310 --> 00:01:59,290 DSN, which is the prefix, which is going to be SQLite. 32 00:01:59,290 --> 00:02:04,620 And we can see more info here, and to access a database on disk, 33 00:02:04,620 --> 00:02:10,070 which is what we have, we would append the absolute path to the DSN prefix. 34 00:02:10,070 --> 00:02:14,220 Okay, so taking all that in, let's go make it work. 35 00:02:14,220 --> 00:02:15,740 Head back over to your workspaces. 36 00:02:15,740 --> 00:02:16,610 You can leave this open. 37 00:02:16,610 --> 00:02:18,550 I'm gonna go ahead and close it. 38 00:02:18,550 --> 00:02:21,520 And now, I'm gonna go ahead and bring my console down. 39 00:02:21,520 --> 00:02:23,260 If it's still there, you can just tuck it away. 40 00:02:23,260 --> 00:02:25,560 Or if you want, you can do Hide Console. 41 00:02:27,250 --> 00:02:32,240 I'm gonna open up my index.php file, scroll to the very top, and 42 00:02:32,240 --> 00:02:34,950 here I'm gonna open up some standard PHP tags, and 43 00:02:34,950 --> 00:02:38,690 we'll add our connection code to start with, here in the index file. 44 00:02:43,880 --> 00:02:44,700 Go ahead and close it. 45 00:02:46,910 --> 00:02:51,070 And inside here, we're gonna wanna do a couple of things. 46 00:02:51,070 --> 00:02:54,410 The very first thing that we're gonna wanna do, 47 00:02:54,410 --> 00:02:58,290 is to enable our error reporting. 48 00:02:58,290 --> 00:03:02,405 And to do that, we're going to first add a comment, and that comment is 49 00:03:02,405 --> 00:03:06,950 gonna remind us to get rid of this code before we put it in production, because we 50 00:03:06,950 --> 00:03:12,170 don't wanna display errors to just any user, it's just for development purposes. 51 00:03:12,170 --> 00:03:16,330 So I'm gonna say remove before flight. 52 00:03:18,130 --> 00:03:22,390 Okay, in the next line of code, we're going to call a built-in PHP 53 00:03:22,390 --> 00:03:27,448 function called ini_set, so ini_set. 54 00:03:27,448 --> 00:03:31,870 And then in here, we're gonna have two different strings. 55 00:03:31,870 --> 00:03:35,244 The first string is what we wanna set, which is display_errors. 56 00:03:38,530 --> 00:03:41,590 And that is display_errors. 57 00:03:41,590 --> 00:03:45,810 And then we're gonna put another string, which is what we wanted on or off. 58 00:03:45,810 --> 00:03:47,940 And in our case, we want it on. 59 00:03:47,940 --> 00:03:51,130 Close our line with a semicolon and that's our very first step that we 60 00:03:51,130 --> 00:03:55,230 have error reporting or displaying errors to our screen on. 61 00:03:55,230 --> 00:03:59,360 The next thing we're gonna do is create our PDO object. 62 00:03:59,360 --> 00:04:05,510 We do that through object oriented code by instantiating a new PDO object. 63 00:04:05,510 --> 00:04:10,012 So, first we give it a variable name to store the object in, so 64 00:04:10,012 --> 00:04:12,740 $ and I'll go with db for database. 65 00:04:14,210 --> 00:04:18,580 The next is the keyword new to create a new object. 66 00:04:18,580 --> 00:04:22,310 And then our class, which is capital P-D-O, and then open and 67 00:04:22,310 --> 00:04:25,290 close parenthesis and close our line. 68 00:04:25,290 --> 00:04:31,440 So inside of our PDO, the first thing that happens is we need a DSN. 69 00:04:31,440 --> 00:04:34,400 And for us, if we recall, it was SQLite, 70 00:04:34,400 --> 00:04:39,820 followed by a colon, followed by the absolute path of the database on disk. 71 00:04:39,820 --> 00:04:47,620 So, head back a couple, open up some strings, and then we're gonna do sqlite:, 72 00:04:47,620 --> 00:04:50,910 in our case, it's in the same folder, so 73 00:04:50,910 --> 00:04:58,620 we're gonna do ./ and then database.db. 74 00:04:58,620 --> 00:05:00,380 Okay. We're gonna save that, 75 00:05:00,380 --> 00:05:05,500 go down to the next line, and just to see that we've created our new object, 76 00:05:05,500 --> 00:05:11,200 we're gonna do a var dump for the actual DB object that we created. 77 00:05:11,200 --> 00:05:12,035 So let's do that now. 78 00:05:12,035 --> 00:05:17,900 var_dump, open and close our parentheses, 79 00:05:19,470 --> 00:05:25,500 and then inside our object $db, close our line with a semi colon, hit Save. 80 00:05:25,500 --> 00:05:28,910 I'm gonna go ahead and hit die after this. 81 00:05:30,130 --> 00:05:34,740 That will stop the actual rest of the file from loading, hit Save and 82 00:05:34,740 --> 00:05:36,100 then click on our Preview icon. 83 00:05:37,940 --> 00:05:41,350 Okay, so here we are seeing it's dumping a object. 84 00:05:41,350 --> 00:05:43,870 It is a PDO object. 85 00:05:43,870 --> 00:05:48,490 If we were maybe to, perhaps, make a mistake, maybe a spelling error. 86 00:05:48,490 --> 00:05:50,160 So let's add a couple extra's As in here. 87 00:05:51,260 --> 00:05:55,620 Save our file, and then re-hit the icon. 88 00:05:55,620 --> 00:06:00,200 You might get this, which is a fatal error, uncaught exception, and 89 00:06:00,200 --> 00:06:01,230 then PDO exception. 90 00:06:01,230 --> 00:06:05,380 Well, we don't want uncaught exceptions, we don't want that accidentally happening, 91 00:06:05,380 --> 00:06:09,930 so the next thing for us to do is a little bit of error handling. 92 00:06:09,930 --> 00:06:14,210 But before we do the error handling, let me go ahead and fix my error. 93 00:06:14,210 --> 00:06:19,370 So I'm gonna go in close this and remove some of these As. 94 00:06:19,370 --> 00:06:22,947 Save and then re-preview. 95 00:06:22,947 --> 00:06:24,020 All right, great. 96 00:06:24,020 --> 00:06:25,700 Now let's move on to some error handling.