Retrieving the Result Set3:31 with Alena Holligan
In this video we’ll explore the PDO **statement** object and how it returns our results. PDO statement objects represents a prepared statement and, after the statement is executed, an associated result set. We access the result set using a method to “fetch” the data.
We have code in place that connects to the database and runs a query against it. 0:00 It's now time to work with the results. 0:04 We have those results in a variable named results. 0:07 However, that variable is not a simple array like you might think. 0:10 It's actually a new kind of php object. 0:15 Let's take a look at the documentation. 0:18 >> Let's go back to workspaces and the connection.php file. 0:20 We connect to the database and if that successful, we run our query. 0:24 If that query is successful, 0:29 then we have the return value of the query method in a variable named results. 0:30 Let's look in more detail at this results variable. 0:34 Let's remove this temporary success message. 0:38 And then we'll jump down below our try catch block. 0:42 We only need to put the code that actually interacts with the database 0:45 in that try catch block. 0:48 Let's display information about this results variable to the screen with 0:50 the var_dump. 0:54 Now let's go back in the browser. 0:58 You can see that the variable is an object. 1:02 It's a different kind of object than what we've seen so far. 1:05 It's not an object of the php mailer class, nor an object of the pdo class. 1:08 It's a pdo statement object. 1:13 Remember, that an object is a collection of variables, called properties, and 1:15 functions, called methods. 1:19 All bundled together when you display information about an object to the screen 1:21 like this you'll see its list of properties. 1:26 This object has one property query string. 1:30 It contains a query string we just ran. 1:33 But how would we find which methods this object has? 1:36 The pdo statement is a native class in php. 1:40 So we can find details in the php manual. 1:43 Lets perform a search for pdo statement. 1:46 [NOISE] The first result is a link to the php manual. 1:48 Looking at the instructions, you can see that this object represents a prepared 1:54 statement, and after that statement is executed and associated, result set. 1:59 We'll talk more about prepared statements a little later. 2:04 Right now, let's look at the result set. 2:07 Here's a list of methods. 2:11 These methods are available for all objects of this class. 2:13 Some of these have names that might look obscure. 2:18 Like bindParam. 2:20 But will look more in-depth into the methods will be using in this project. 2:22 Scrolling down past this list of methods. 2:26 You can see all the method names here with a little bit of a description. 2:29 Let's see. 2:33 This fetch all method looks promising. 2:33 It returns an array containing all the result set rows. 2:37 In other words it organizes the data the database returns into an array. 2:41 Let's go back to our code. 2:47 We'll call the fetch all method without any arguments. 2:49 Now let's go back in the browser and take a look at these results. 2:56 The array has 36 elements, one for each item in the database. 2:59 If we view the page source, we can see this a little more clearly. 3:03 Each of our 36 elements then has an internal array 3:08 with each of the attributes of that item. 3:11 We've now written code that uses php data objects to query the database and 3:14 to load the results in a php array. 3:18 Although this array does resemble our catalog array in the next video 3:21 we'll be fine tuning these results to allow us to swap them out for 3:26 our current catalog array. 3:29
You need to sign up for Treehouse in order to download course files.Sign up