1 00:00:00,270 --> 00:00:05,150 Let's go back to our films.php file, and then how can we tidy this up. 2 00:00:05,150 --> 00:00:07,930 Well, we're doing a tri-catch here that's good, but 3 00:00:07,930 --> 00:00:14,300 what we're not checking is that what do we do if the ID is not set, 4 00:00:14,300 --> 00:00:19,070 and then if it's not set why would we wanna run this code here? 5 00:00:19,070 --> 00:00:24,000 So actually let's just get rid of all of this code that's in here, and 6 00:00:24,000 --> 00:00:26,640 put it inside of our if statement. 7 00:00:26,640 --> 00:00:29,390 We're actually gonna grab all the way through to line 19 cuz 8 00:00:29,390 --> 00:00:33,940 there certainly no point of us doing this if we don't have it. 9 00:00:33,940 --> 00:00:37,766 So we'll go ahead and hit paste in here to drop it in and 10 00:00:37,766 --> 00:00:39,903 I'm gonna do a quick tidy up. 11 00:00:39,903 --> 00:00:45,504 [BLANK_AUDIO] 12 00:00:45,504 --> 00:00:49,261 Okay, so, at this point right now if we look at this code, 13 00:00:49,261 --> 00:00:53,324 it says if we have an ID, if it's there, it's not empty, 14 00:00:53,324 --> 00:00:59,340 then we'll go ahead and set our, film ID, make sure it's an integer value. 15 00:00:59,340 --> 00:01:04,030 And then we'll run a query on our database by going through the prepare statement, 16 00:01:04,030 --> 00:01:07,460 the bind parameter statement and execute so on so forth. 17 00:01:07,460 --> 00:01:10,930 But what we're concerned with is if nothing is returned if it 18 00:01:10,930 --> 00:01:16,920 doesn't actually have anything in it whatsoever, or if the ID is empty. 19 00:01:16,920 --> 00:01:21,620 So if the ID is not set then we don't wanna run the code, so that's what this is 20 00:01:21,620 --> 00:01:25,260 covering but now we wanna make sure if we don't actually have a film. 21 00:01:25,260 --> 00:01:27,830 So let's go down here to the bottom and 22 00:01:27,830 --> 00:01:32,950 see that if we don't have a film it's not gonna echo any kind of title back to us. 23 00:01:32,950 --> 00:01:36,200 So let's put an if statement down here to test whether or 24 00:01:36,200 --> 00:01:38,870 not that film has anything in it. 25 00:01:40,080 --> 00:01:44,060 Let's do that by making this php tag a little more encompassing. 26 00:01:44,060 --> 00:01:46,870 We'll actually get rid of the h2 on either side of it. 27 00:01:49,100 --> 00:01:53,470 And then bring it down a few lines so we can just concentrate on the code. 28 00:01:55,690 --> 00:01:56,900 Okay. 29 00:01:56,900 --> 00:02:01,020 So now here at the bottom of our file we're gonna echo film title. 30 00:02:01,020 --> 00:02:04,210 Well, we wanna make sure that there actually is a film title. 31 00:02:04,210 --> 00:02:08,370 so, if there isn't a film title or there's no film at all, then we wanna return 32 00:02:08,370 --> 00:02:13,350 a message that say, sorry there's no film could be found with the specified ID. 33 00:02:13,350 --> 00:02:17,750 The way that we can do that is to just check if the film object itself, or 34 00:02:17,750 --> 00:02:19,440 the film variable. 35 00:02:19,440 --> 00:02:22,750 Is is set, it actually has something in it. 36 00:02:22,750 --> 00:02:29,592 We can do that with the isset function of php, so if I actually type php isset, 37 00:02:29,592 --> 00:02:34,560 which is all one word, here's the manual on php.net. 38 00:02:34,560 --> 00:02:38,685 And then we're looking to see if a variable is set and not null, or 39 00:02:38,685 --> 00:02:40,110 basically not empty. 40 00:02:40,110 --> 00:02:44,924 All right, so close that out and then we'll go back to our films, and 41 00:02:44,924 --> 00:02:50,850 then we'll say if open up our parents, and then open up our curly braces. 42 00:02:50,850 --> 00:02:54,220 We'll close out our curly braces, and then, what are we testing? 43 00:02:54,220 --> 00:02:56,940 We're testing that is set. 44 00:02:58,360 --> 00:03:00,230 Open and close parens again. 45 00:03:00,230 --> 00:03:02,720 And then do $film. 46 00:03:02,720 --> 00:03:05,210 Okay, so we wanna make sure that it's actually set and 47 00:03:05,210 --> 00:03:07,560 it's gonna return something to us. 48 00:03:07,560 --> 00:03:10,040 If it's empty, we wanna do something else. 49 00:03:10,040 --> 00:03:14,080 Let's make sure that it's working before we test our actual error case. 50 00:03:14,080 --> 00:03:19,600 So here we'll go back and do, number nine again, hit enter. 51 00:03:19,600 --> 00:03:20,480 Okay, that's working. 52 00:03:20,480 --> 00:03:23,580 It's still returning it to us, but now it's this normal text. 53 00:03:23,580 --> 00:03:27,160 That's because we lost our h2 that was here before. 54 00:03:27,160 --> 00:03:29,160 What we're gonna want to, the h2 for 55 00:03:29,160 --> 00:03:33,340 the film title or our statement that says we can't find a film. 56 00:03:33,340 --> 00:03:34,770 So I'm going to put that outside here. 57 00:03:36,350 --> 00:03:38,064 Okay. And make sure we close it 58 00:03:38,064 --> 00:03:40,296 out after our php tags are closed. 59 00:03:40,296 --> 00:03:44,346 [BLANK_AUDIO] 60 00:03:44,346 --> 00:03:46,380 All right. There's our h2. 61 00:03:46,380 --> 00:03:48,200 Save it, let's refresh our page here. 62 00:03:49,810 --> 00:03:50,450 Okay. Much better. 63 00:03:50,450 --> 00:03:53,550 That's back to the h2 that we were expecting. 64 00:03:53,550 --> 00:03:56,260 And, now, we'll do our else statement. 65 00:03:57,560 --> 00:03:59,520 So, else, what do we wanna do, 66 00:03:59,520 --> 00:04:03,820 we want to return some kind of a message that says, sorry it cannot be found. 67 00:04:03,820 --> 00:04:04,873 So we'll do an echo. 68 00:04:04,873 --> 00:04:08,853 [BLANK_AUDIO] 69 00:04:08,853 --> 00:04:11,925 Add our quotes, close it out with a semicolon, and 70 00:04:11,925 --> 00:04:15,350 then whatever we wanna return to our user in the h2. 71 00:04:15,350 --> 00:04:22,241 So we'll say, sorry, comma, no film was found 72 00:04:22,241 --> 00:04:28,800 with the provided ID. 73 00:04:28,800 --> 00:04:30,572 Yeah, that's good enough for now. 74 00:04:30,572 --> 00:04:33,780 All right, let's save that and refresh our page. 75 00:04:33,780 --> 00:04:35,000 Okay, still works, but 76 00:04:35,000 --> 00:04:39,990 if we go with a number that's outside of this, still not returning what we need. 77 00:04:39,990 --> 00:04:40,550 Okay. 78 00:04:42,130 --> 00:04:47,630 So film must be set so where are we gonna need to modify our code? 79 00:04:47,630 --> 00:04:49,960 Let's see what film looks like here. 80 00:04:49,960 --> 00:04:57,770 If we go up to line 16, clearly film is set after our results test is run. 81 00:04:57,770 --> 00:05:04,390 All right, so we'll go here and let's just do a var_dump. 82 00:05:04,390 --> 00:05:06,395 Some rudimentary debugging here for you. 83 00:05:06,395 --> 00:05:11,830 $film, hit the semicolon, 84 00:05:11,830 --> 00:05:15,830 save it and go back up and hit refresh. 85 00:05:15,830 --> 00:05:18,680 Okay, all is returning to us, a boolean false. 86 00:05:18,680 --> 00:05:22,130 Okay, so false is technically set. 87 00:05:22,130 --> 00:05:24,920 It's a boolean, it's not considered empty. 88 00:05:24,920 --> 00:05:28,440 So the isset will not work for us. 89 00:05:28,440 --> 00:05:30,528 So that code really isn't what we want. 90 00:05:30,528 --> 00:05:32,520 Okay, so let's switch back over to our code, 91 00:05:32,520 --> 00:05:38,000 we don't need this if statement down here for this particular instance. 92 00:05:38,000 --> 00:05:41,560 We'll just leave it in we're gonna need an if l statement I'm sure later. 93 00:05:41,560 --> 00:05:43,580 But we're not gonna need this particular message. 94 00:05:43,580 --> 00:05:48,540 So I'll just remove the else for now and leave this in place. 95 00:05:48,540 --> 00:05:50,235 Then we'll go up here and 96 00:05:50,235 --> 00:05:56,400 this is where were going to want to test whether or not film is what we expect. 97 00:05:56,400 --> 00:06:00,000 So right here under line sixteen we'll create a new line and 98 00:06:00,000 --> 00:06:02,860 we're gonna do another test or another conditional. 99 00:06:02,860 --> 00:06:07,510 Now the conditional that we're testing here is that we've taken the ID. 100 00:06:07,510 --> 00:06:08,650 We've passed it through. 101 00:06:08,650 --> 00:06:11,430 We've, you know, requested it from the database. 102 00:06:11,430 --> 00:06:15,380 It's returning to us a false because nothing was found. 103 00:06:15,380 --> 00:06:21,460 We want to test to see whether or not film is false, so we can return a message. 104 00:06:21,460 --> 00:06:26,900 So we'll do if, and then we'll do $film equals, 105 00:06:26,900 --> 00:06:29,700 equals, capital FALSE here for our keyword. 106 00:06:30,970 --> 00:06:34,290 Okay. And then if it is false, 107 00:06:34,290 --> 00:06:38,320 then we wanna return a message that says the film could not be found. 108 00:06:38,320 --> 00:06:43,430 Now we can do that by actually using the code that already exists here. 109 00:06:43,430 --> 00:06:44,730 It's in an H2. 110 00:06:44,730 --> 00:06:46,570 It's film, film title. 111 00:06:46,570 --> 00:06:49,890 So we can just set our film title if we want, or 112 00:06:49,890 --> 00:06:51,270 we could return a message and die. 113 00:06:51,270 --> 00:06:55,030 Let's just return a message and then kill the process for now. 114 00:06:55,030 --> 00:07:01,340 So we will echo and then we're gonna use what we previously put down there before. 115 00:07:01,340 --> 00:07:07,200 So, sorry a film could not be 116 00:07:07,200 --> 00:07:13,440 found with the provided ID. 117 00:07:13,440 --> 00:07:14,030 All right. 118 00:07:14,030 --> 00:07:16,410 Now, let's see if this works as we expect, so 119 00:07:16,410 --> 00:07:20,040 we'll go back over to our page here, after saving. 120 00:07:20,040 --> 00:07:21,460 And we'll hit refresh. 121 00:07:21,460 --> 00:07:25,400 That says, sorry, a film could not be found with the provided ID.That's good, 122 00:07:25,400 --> 00:07:28,550 except we wanna kill the process at the end by typing, die. 123 00:07:30,990 --> 00:07:35,430 Okay, so we'll hit refresh on this, and now it says sorry, a film could not be 124 00:07:35,430 --> 00:07:39,680 found with the provided ID, and then we go back here and hit number nine like we 125 00:07:39,680 --> 00:07:45,060 had before, and now we're back to ALABAMA DEVIL which is ID nine. 126 00:07:45,060 --> 00:07:49,300 Okay, so just as a review here, we have gone through, 127 00:07:49,300 --> 00:07:52,950 we've got our binded and prepared parameters. 128 00:07:52,950 --> 00:07:56,230 We execute our statement, and then we go down here and 129 00:07:56,230 --> 00:08:01,470 make sure that the film is actually there, if something actually exists. 130 00:08:01,470 --> 00:08:04,150 And if it doesn't, then we return a message to kill the process. 131 00:08:04,150 --> 00:08:05,600 We could definitely do more there, 132 00:08:05,600 --> 00:08:09,830 a little bit more user interface friendly error messages. 133 00:08:09,830 --> 00:08:13,070 But for now, this is great as an example for you. 134 00:08:13,070 --> 00:08:14,820 Now the workspace here that's set up for 135 00:08:14,820 --> 00:08:17,510 you has the database, it has all these tables. 136 00:08:17,510 --> 00:08:20,910 Just as an example here we'll do one last little thing. 137 00:08:20,910 --> 00:08:24,200 Instead of it saying if it is set film, 138 00:08:24,200 --> 00:08:28,470 I just want to change the code just here for a second. 139 00:08:28,470 --> 00:08:33,136 And instead of echoing it out, I'll actually continue to echo it out, but 140 00:08:33,136 --> 00:08:35,876 afterwards, I'm gonna do a print_r, and 141 00:08:35,876 --> 00:08:40,044 pass through the actual film object, or the film array variable. 142 00:08:40,044 --> 00:08:43,764 [BLANK_AUDIO] 143 00:08:43,764 --> 00:08:47,990 Let's save this, and go back and hit refresh. 144 00:08:47,990 --> 00:08:51,380 Okay, if you see here, we have our array, film ID, 145 00:08:51,380 --> 00:08:56,560 the description, all the information about the film. 146 00:08:56,560 --> 00:08:59,840 We're really, we're only needing the film ID. 147 00:08:59,840 --> 00:09:04,680 So something I say that I would do to continue learning on this, 148 00:09:04,680 --> 00:09:10,000 would be to take what's existing now and, and build a little something out. 149 00:09:10,000 --> 00:09:15,480 Maybe run your query so it's just getting the title and maybe just the ID or 150 00:09:15,480 --> 00:09:19,760 just a few other small items instead of everything about the film. 151 00:09:19,760 --> 00:09:26,550 Perhaps on our main page on our index page, instead of doing everything here, 152 00:09:26,550 --> 00:09:30,220 enlisting a thousand of them, maybe try to build in some pagination. 153 00:09:30,220 --> 00:09:33,360 Do, you know, 10 per page or 20 per page. 154 00:09:33,360 --> 00:09:34,480 Feel free to play around with it. 155 00:09:34,480 --> 00:09:36,660 The workspace is yours to use and enjoy. 156 00:09:36,660 --> 00:09:38,280 I hope you've enjoyed the course. 157 00:09:39,410 --> 00:09:43,700 Now you have a good base understanding of how to work with a database in php 158 00:09:43,700 --> 00:09:45,890 using php data objects. 159 00:09:45,890 --> 00:09:49,830 Practice is very important to learning more about how we work with databases, and 160 00:09:49,830 --> 00:09:52,190 what else is possible with PDO. 161 00:09:52,190 --> 00:09:54,730 That said, I would suggest you take a look at the notes for 162 00:09:54,730 --> 00:09:58,260 this course, and read all the documentation thoroughly. 163 00:09:58,260 --> 00:10:02,410 Take some time to use work spaces, and practice with the example database, and 164 00:10:02,410 --> 00:10:05,120 perhaps look at some open source php projects. 165 00:10:05,120 --> 00:10:07,950 To see how they handle working with databases. 166 00:10:07,950 --> 00:10:11,580 The next logical step after understanding how PDO works, is to 167 00:10:11,580 --> 00:10:16,830 look into further database abstraction with ORMs, or Object Relational Mappers. 168 00:10:16,830 --> 00:10:22,110 Such as Doctrine, which you can find at doctrine-project.org. 169 00:10:22,110 --> 00:10:25,420 See you next time, and remember to work hard, and have a little joy.