1 00:00:00,218 --> 00:00:04,783 [MUSIC] 2 00:00:04,783 --> 00:00:07,008 Queries, are one of the most used items, 3 00:00:07,008 --> 00:00:11,030 out of all of the interfacing we'll want to do with our PDO object. 4 00:00:11,030 --> 00:00:14,380 Let's take a closer look at queries, using PDO and 5 00:00:14,380 --> 00:00:16,920 what these queries will return to us. 6 00:00:16,920 --> 00:00:21,690 The time has now come for us to query our database and 7 00:00:21,690 --> 00:00:25,260 get a set of films, that return back to us directly from the database and 8 00:00:25,260 --> 00:00:27,210 we'll store it into a variable. 9 00:00:27,210 --> 00:00:30,900 The way that we're gonna do that is by running a method that is built in to 10 00:00:30,900 --> 00:00:32,670 the PDO object. 11 00:00:32,670 --> 00:00:36,080 So let's take a look at the docs again for the PDO class. 12 00:00:36,080 --> 00:00:38,290 We'll just go and open up a new tab. 13 00:00:38,290 --> 00:00:43,900 Pdo, php, and the first one should be the pdo manual. 14 00:00:43,900 --> 00:00:46,490 That's good. And this is the introduction. 15 00:00:46,490 --> 00:00:51,660 We're just gonna scroll down here and click, directly on the pdo class. 16 00:00:53,420 --> 00:00:54,700 Okay, scroll down. 17 00:00:54,700 --> 00:00:56,790 Take a look at the methods. 18 00:00:56,790 --> 00:00:57,620 The one that we're gonna want, 19 00:00:57,620 --> 00:01:01,680 is over here on the right, about three quarters of the way down, called query. 20 00:01:01,680 --> 00:01:06,010 So, it's a public, it will return a pdo statement, and 21 00:01:06,010 --> 00:01:08,450 it is query, is the method name. 22 00:01:08,450 --> 00:01:14,290 And then the single argument is a string, or the SQL statement that you wanna run. 23 00:01:14,290 --> 00:01:17,090 You click on the query link, it'll say just the same, 24 00:01:17,090 --> 00:01:23,180 it executes a SQL statement, returning a result set as a PDOStatement object. 25 00:01:23,180 --> 00:01:26,980 So it's gonna return to us a new object called a PDOStatement. 26 00:01:26,980 --> 00:01:32,990 So let's start, by coming over here and running our query on the db object. 27 00:01:32,990 --> 00:01:36,190 So, we can do that right here in our index.php file. 28 00:01:36,190 --> 00:01:40,370 Let's go ahead and clean up our file a little bit so 29 00:01:40,370 --> 00:01:44,280 that we only have the information that we need in that file. 30 00:01:44,280 --> 00:01:48,290 So for instance, on the index page, we're gonna wanna this database connection, but 31 00:01:48,290 --> 00:01:51,630 we might want that connection on other pages, as well. 32 00:01:51,630 --> 00:01:54,940 So we'll create a new file, called database.php. 33 00:01:54,940 --> 00:02:04,060 And so let's do a new file, database.php. 34 00:02:04,060 --> 00:02:05,630 Open up that file and 35 00:02:05,630 --> 00:02:08,640 we're gonna actually copy the information from this first section. 36 00:02:08,640 --> 00:02:10,670 I'm actually just gonna cut it all the way out. 37 00:02:11,682 --> 00:02:15,400 All right, save our index file, and I'm gonna paste this in. 38 00:02:15,400 --> 00:02:19,850 So, this is still setting our errors to on, we still want that for now. 39 00:02:19,850 --> 00:02:25,340 It's gonna be our try-catch block it's gonna have a db object here on line seven, 40 00:02:25,340 --> 00:02:29,500 and at the very end of this file, the db object will still exist. 41 00:02:29,500 --> 00:02:34,490 The key here, is that we want to include, or in our case, called require, 42 00:02:34,490 --> 00:02:40,030 require this file to be present inside of the index.php file. 43 00:02:40,030 --> 00:02:46,040 So, let's open up our php tags again, we'll close them out. 44 00:02:48,060 --> 00:02:53,750 And then, we'll keep just the code specific to this index file in here. 45 00:02:53,750 --> 00:02:59,240 We'll do that by using the internal function require_once and 46 00:03:00,750 --> 00:03:09,559 then require once, needs a file name, which is for us is gonna be database.php. 47 00:03:10,730 --> 00:03:12,400 All right, close that out. 48 00:03:12,400 --> 00:03:16,590 And now we have required that the database file be there. 49 00:03:16,590 --> 00:03:20,070 The way we can make sure that it's requiring the file, that it's including 50 00:03:20,070 --> 00:03:26,950 the file inside the index dot PHP is to now var_dump and die our actual DB object. 51 00:03:26,950 --> 00:03:29,050 So let's do that just as an example. 52 00:03:33,500 --> 00:03:37,160 Dollar sign db as our object or our pdo object, and 53 00:03:37,160 --> 00:03:42,520 then we're on the statement die and that will stop the process. 54 00:03:42,520 --> 00:03:47,460 So, we'll close this manual out and we'll go here and hit our preview icon. 55 00:03:47,460 --> 00:03:50,850 And now you'll see that we are dumping and dieing our PDO object. 56 00:03:50,850 --> 00:03:55,220 But this time our database file is separately included or 57 00:03:55,220 --> 00:03:57,590 required into our index file. 58 00:03:57,590 --> 00:04:01,300 So, now that we don't need this code any longer, I'm gonna actually close it out. 59 00:04:01,300 --> 00:04:05,860 And we'll continue running our query from here at the index.php file. 60 00:04:07,330 --> 00:04:13,110 So let's remove lines 5 and 6, and then I'm going to create a new 61 00:04:13,110 --> 00:04:18,010 connection to the database, or rather a new method to the database, called query. 62 00:04:18,010 --> 00:04:24,660 And we're gonna do that by doing $db and then the object operator to query. 63 00:04:25,720 --> 00:04:29,950 Open and close our parenthesis and close it out with a semicolon. 64 00:04:29,950 --> 00:04:33,950 Now if you recall from the documentation it takes a single string. 65 00:04:33,950 --> 00:04:39,850 So in our case our single string is going to be for this particular page. 66 00:04:39,850 --> 00:04:42,010 Select * from film. 67 00:04:42,010 --> 00:04:44,630 So we're taking and just grabbing everything. 68 00:04:44,630 --> 00:04:47,480 Not very efficient at the moment, but let's just, just for 69 00:04:47,480 --> 00:04:54,360 an example, we'll do select, star, film. 70 00:04:54,360 --> 00:04:55,830 Okay. Now that is our query. 71 00:04:55,830 --> 00:04:59,060 Now, it returns to us an object. 72 00:04:59,060 --> 00:05:01,420 So, we actually need to store this somewhere. 73 00:05:01,420 --> 00:05:02,850 So I'm gonna call it results. 74 00:05:06,560 --> 00:05:07,070 All right. 75 00:05:07,070 --> 00:05:09,360 And then on the next line, let's dump it and 76 00:05:09,360 --> 00:05:13,590 see what the results object looks like. 77 00:05:13,590 --> 00:05:18,260 So what we'll do, var_dump and then pass the results. 78 00:05:22,040 --> 00:05:23,750 Close it out. 79 00:05:23,750 --> 00:05:26,130 And then die at the next statement, or the next line. 80 00:05:29,130 --> 00:05:32,910 Save our file, and then go back and hit preview again. 81 00:05:32,910 --> 00:05:37,030 So here we'll see our object, the PDOStatement object, and 82 00:05:37,030 --> 00:05:39,490 then it says there is one item in there, and 83 00:05:39,490 --> 00:05:44,180 that is the query string, which is select star from film. 84 00:05:44,180 --> 00:05:49,450 That's great but, it's really not giving us a whole lot of information so 85 00:05:49,450 --> 00:05:55,370 we're gonna actually need to work with this result set, more in detail and 86 00:05:55,370 --> 00:05:59,910 we also need to manage our errors because let's say for instance our query string is 87 00:05:59,910 --> 00:06:05,860 wrong and we just say like D from film, which would not be correct. 88 00:06:05,860 --> 00:06:09,110 And then we go back and refresh, we have an uncaught exception. 89 00:06:09,110 --> 00:06:13,660 And like we learned earlier, we don't want uncaught exceptions in our code. 90 00:06:13,660 --> 00:06:16,050 So we need to wrap this in a try and 91 00:06:16,050 --> 00:06:19,950 catch block we'll do that next and work with the result set.