Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have successfully run a query, we need to take a closer look at what is returned to us by our query method. All of the return data comes back to us in the form of a PDO Statement object.
-
0:00
Now that we have successfully run a query,
-
0:02
we need to take a closer look at what is returned to us by our query method.
-
0:06
All of the return data comes back to us in the form of a PDO statement object.
-
0:12
Okay continuing, we're going to get rid of this exception that we have here.
-
0:17
It's refreshed I've already fixed it in the file.
-
0:19
So refresh this and we're back to our PDO statement object showing the query string.
-
0:25
And we need to extract the results from this.
-
0:29
We wanna get, get all of our films out of our PDO statement object.
-
0:34
And the first place I would say check is we go to our documentation and
-
0:38
look up the PDOStatement object.
-
0:40
I'm just gonna actually copy this and paste it and hit enter, and
-
0:46
that will take us to the manual page for the PHP PDOStatement.
-
0:51
So, here is our PDOStatement class.
-
0:54
It, says it represents a prepared statement and
-
0:57
after it is executed, the associated results set, so
-
1:01
that is what we're gonna work with is our associated results set.
-
1:05
We're gonna go on down and
-
1:06
look at the methods and the method that we're gonna want here.
-
1:11
Let's say we could do fetch, fetch, fetch all.
-
1:14
Yeah, that sounds good, fetch everything.
-
1:16
This says it returns an array containing all the result set rows.
-
1:21
So it's going to return to us an array, and
-
1:25
it doesn't have any required arguments.
-
1:26
So let's go ahead and run that method.
-
1:29
On our results object which is a instance of the PDOStatement object.
-
1:35
And back over to our code, we're gonna go down here and
-
1:38
we're gonna work with this results set.
-
1:41
So here I'm gonna continue to var_dump and
-
1:43
then dive, our result set, but we want to use the fetch all method.
-
1:49
Before we dig into our fetch all method and
-
1:51
see what it's gonna return to us, we wanna get rid of that error or
-
1:55
the possibility of an exception being thrown by the query being incorrect.
-
2:01
The way we're gonna do that is with wrapping it in a try catch block.
-
2:05
So we'll go ahead up here to line four,
-
2:07
come down a little bit, and wrap this in a try block.
-
2:11
Try no arguments.
-
2:14
I'm gonna tab this in.
-
2:18
All right.
-
2:19
Close this guy out, and then open up catch.
-
2:24
And we are still going to catch the Exception class, and
-
2:29
pass it through to an object $e, close it out.
-
2:34
And in here the same thing as before.
-
2:36
We're just going to echo out our exception object, which is $e.
-
2:42
And the method that we're going to use is going to be
-
2:45
the same as before, getMessage.
-
2:50
Okay, pass that through just to make sure that our tri catch block is working.
-
2:55
We'll again, get rid of our correct statement and then refresh.
-
3:00
And then now you'll see it says General Error.
-
3:03
That's saying it's our syntax error.
-
3:05
That's exactly what we wanted to see,
-
3:07
except we didn't want to see the rest of this.
-
3:10
So we need to add a die statement to the end of this.
-
3:16
Okay, so let's refresh again.
-
3:19
There we go much better.
-
3:20
It just kills it, and gives us the actual error itself.
-
3:23
That's exactly what we're looking for.
-
3:26
All right, let's go back in and fix our statement here.
-
3:31
Select, all right save that.
-
3:35
Double check that we're back to working.
-
3:37
And we are, great.
-
3:39
Now instead of just dumping the results,
-
3:41
we're going to use that method that we looked at which is fetch all.
-
3:46
So, object operator, fetch, capital A for fetchAll.
-
3:51
And then open and close parens with no arguments and let's go see what it does.
-
3:56
Hit refresh.
-
3:58
All right. We have an array with a thousand results.
-
4:02
That's a lot.
-
4:02
And it's really hard to look at right now,
-
4:04
so let's put some pre tags in here so we can see this a bit better.
-
4:09
Right before here we'll go ahead and do, echo.
-
4:15
And then open up some tags for pre.
-
4:17
Close it out.
-
4:18
I'm gonna copy this line.
-
4:22
I'm gonna head down here and close out my pre tag.
-
4:26
Let's go back and see if that makes it looks a little nicer.
-
4:31
Much better.
-
4:32
So, looking at our result set here again, we have a thousand results.
-
4:35
A lot of results.
-
4:36
We definitely would wanna paginate this in a production environment, but
-
4:39
again, this is just for examples and not to be put into production.
-
4:43
What do we have?
-
4:44
We have each one of the films it seems.
-
4:48
And a lot of information that looks duplicate.
-
4:50
Okay, so the only difference between them is the key.
-
4:54
So you see we have a film id and that's equal to a string of one.
-
4:59
And then we have a numerically indexed zero, so
-
5:03
the first index zero, with an id of one.
-
5:07
Double checking title.
-
5:09
Key of title, that's the academy dinosaur.
-
5:12
All right?
-
5:13
And then index one, which is a second key.
-
5:17
And that's the same, title as well.
-
5:19
So it looks like we have something indexed by column name and
-
5:22
something indexed by a numeric index.
-
5:25
So let's go back to our PDO statement, fetchAll.
-
5:29
Look at the manual, and the first optional argument is the fetch style,
-
5:35
which takes another argument called fetch argument.
-
5:38
Okay, so let's look down here at fetch style.
-
5:43
So it says fetch_style controls the content of the returned array as
-
5:47
documented in the Fetch documents.
-
5:49
So let's go to Fetch, see what that says.
-
5:53
Okay this is a result set, single row, fetch_style parameters.
-
5:59
Okay so the default here, it says, is returns and
-
6:02
array indexed by both column name and zero indexed column number.
-
6:08
That's what we have.
-
6:09
That's our default.
-
6:10
But the option to do just the associative array, is our first argument here.
-
6:15
Great, great. So that's PDO fetch.
-
6:18
ASSOC or associative.
-
6:20
So it returns an array index by column name as a return in your result set.
-
6:25
Let's do that.
-
6:26
So we wanna set fetch style to PDO FETCH_ASSOC.
-
6:30
So I'm gonna copy this here and
-
6:32
I'm gonna pass that through in our code here in our FetchAll statement.
-
6:37
Right here is our first argument.
-
6:39
Gonna paste in that class constant.
-
6:42
Hit Save.
-
6:43
Let's go back to our code and refresh.
-
6:46
Okay.
-
6:47
Still 1,000 results.
-
6:49
That's right. That's exactly what we were looking for.
-
6:52
And now all of them are indexed by column name.
-
6:55
So there's no miscellaneous zero through whatever for the keys.
-
6:59
It's just an associative array for each film, that's awesome.
-
7:03
So now we know that our result set is a set of films indexed by a column name,
-
7:09
so we should be able to build out our code a little bit.
-
7:12
So next we'll do that by providing some links,
-
7:15
work with these results, and create a little bit more of a website.
You need to sign up for Treehouse in order to download course files.
Sign up