Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Queries with PDO6:20 with Hampton Paulk
Queries are one of the most used items out of all of the interfacing we will want to do with our PDO object. Let's take a closer look at queries using PDO and what these queries will return to us.
[MUSIC] 0:00 Queries, are one of the most used items, 0:04 out of all of the interfacing we'll want to do with our PDO object. 0:07 Let's take a closer look at queries, using PDO and 0:11 what these queries will return to us. 0:14 The time has now come for us to query our database and 0:16 get a set of films, that return back to us directly from the database and 0:21 we'll store it into a variable. 0:25 The way that we're gonna do that is by running a method that is built in to 0:27 the PDO object. 0:30 So let's take a look at the docs again for the PDO class. 0:32 We'll just go and open up a new tab. 0:36 Pdo, php, and the first one should be the pdo manual. 0:38 That's good. And this is the introduction. 0:43 We're just gonna scroll down here and click, directly on the pdo class. 0:46 Okay, scroll down. 0:53 Take a look at the methods. 0:54 The one that we're gonna want, 0:56 is over here on the right, about three quarters of the way down, called query. 0:57 So, it's a public, it will return a pdo statement, and 1:01 it is query, is the method name. 1:06 And then the single argument is a string, or the SQL statement that you wanna run. 1:08 You click on the query link, it'll say just the same, 1:14 it executes a sequel statement, returning a result set as a PDOStatement object. 1:17 So it's gonna return to us a new object called a PDOStatement. 1:23 So let's start, by coming over here and running our query on the db object. 1:26 So, we can do that right here in our index.php file. 1:32 Let's go ahead and clean up our file a little bit so 1:36 that we only have the information that we need in that file. 1:40 So for instance, on the index page, we're gonna wanna this database connection, but 1:44 we might want that connection on other pages, as well. 1:48 So we'll create a new file, called database.Php. 1:51 And so lets do a new file, database.php. 1:54 Open up that file and 2:04 we're gonna actually copy the information from this first section. 2:05 I'm actually just gonna cut it all the way out. 2:08 All right, save our index file, and I'm gonna paste this in. 2:11 So, this is still setting our errors to on, we still want that for now. 2:15 It's gonna be our try-catch block it's gonna have a db object here on line seven, 2:19 and at the very end of this file, the db object will still exist. 2:25 The key here, is that we want to include, or in our case, called require, 2:29 require this file to be present inside of the index.php file. 2:34 So, let's open up our php tags again, we'll close them out. 2:40 And then, we'll keep just the code specific to this index file in here. 2:48 We'll do that by using the internal function require_once and 2:53 then require once, needs a file name, which is for us is gonna be database.php. 3:00 All right, close that out. 3:10 And now we have required that the database file be there. 3:12 The way we can make sure that it's requiring the file, that it's including 3:16 the file inside the index dot PHP is to now var dump and die our actual DB object. 3:20 So let's do that just as an example. 3:26 Dollar sign db as our object or our pdo object, and 3:33 then we're on the statement die and that will stop the process. 3:37 So, we'll close this manual out and we'll go here and hit our preview icon. 3:42 And now you'll see that we are dumping and dieing our PDO object. 3:47 But this time our database file is separately included or 3:50 required into our index file. 3:55 So, now that we don't need this code any longer, I'm gonna actually close it out. 3:57 And we'll continue running our query from here at the index.php file. 4:01 So let's remove lines 5 and 6, and then I'm going to create a new 4:07 connection to the database, or rather a new method to the database, called query. 4:13 And we're gonna do that by doing $db and then the object operator to query. 4:18 Open and close our parenthesis and close it out with a semicolon. 4:25 Now if you recall from the documentation it takes a single string. 4:29 So in our case our single string is going to be for this particular page. 4:33 Select start from film. 4:39 So we're taking and just grabbing everything. 4:42 Not very efficient at the moment, but let's just, just for 4:44 an example, we'll do select, star, film. 4:47 Okay. Now that is our query. 4:54 Now, it returns to us an object. 4:55 So, we actually need to store this somewhere. 4:59 So I'm gonna call it results. 5:01 All right. 5:06 And then on the next line, let's dump it and 5:07 see what the results object looks like. 5:09 So what we'll do, var_dump and then pass the results. 5:13 Close it out. 5:22 And then die at the next statement, or the next line. 5:23 Save our file, and then go back and hit preview again. 5:29 So here we'll see our object, the PDOStatement object, and 5:32 then it says there is one item in there, and 5:37 that is the query string, which is select star from film. 5:39 That's great but, it's really not giving us a whole lot of information so 5:44 we're gonna actually need to work with this result set, more in detail and 5:49 we also need to manage our errors because let's say for instance our query string is 5:55 wrong and we just say like D from film, which would not be correct. 5:59 And then we go back and refresh, we have an uncaught exception. 6:05 And like we learned earlier, we don;t want uncaught exceptions in our code. 6:09 So we need to wrap this in a try and 6:13 catch block we'll do that next and work with the result set. 6:16
You need to sign up for Treehouse in order to download course files.Sign up