PHP Data Objects (PDO): Fetch types (Part 3/8)

Sharing buttons:

so we've already looked at returning

data from our database in a you know

sort of associative and numeric array

combined however what happens if we want

to go ahead and only return an

associative array to voids up the size

of this array getting too big or we want

to fetch it as an object perhaps or just

a numeric array and there are others but

we'll we'll look at particularly at one

in the next video which is fetching into

a class with properties being set into a

class but for now what we'll do is we'll

look at just the num so see ative and

the object different ways of fetching

and we'll see how we can do this so at

the moment we're just calling fetch and

by default and the we have a sort of way

of being fetched as fetch both so the

equivalent of doing this says PDO fetch

both so we pass that into the fetch

method and it does exactly the same as

we've already seen here however what we

can do is we can change this to sort of

describe how we want this data to be

returned so for example we just wanted a

numeric away array we could do fetch num

when we refresh now we just get a

numeric array 0 1 2 3 4 4 fields and the

data that relates to them fields if we

just wanted an associative array we just

use fetch a sock for associative and

that just gives us an associative array

now more interestingly and keeping in

line with the fact that this PDO

extension is object oriented we might

want to fetch as an object and that's

usually a preference so we'll change

this to fetch objects at obj for object

and we'll go ahead and print our on it

just to see what this looks like so that

looks like this now what's happened here

is not much has changed because we've

got the same sort of field names here

the same field names here but this is

now an anonymous object and what that

will actually allow us to do is rather

than access the properties by and in an

array way we do it by an object way so

we use the hyphen here and the greater

than sign to access the properties so

now what I would do is I'd go ahead and


the wire loop has we did in the last

video and I'd say while and then placed

this into my while loop so while our

equals this result here then what we

want to do is go ahead and echo our

message and we go ahead and append the

line break on to that so now what we've

got is the ability to output this data

but we've returned it as an object

rather than an array so we don't need to

mess around with saying things like our

message like this we can just directly

access this message property of the

anonymous object as returned when we use

PDO fetch

object now as I mentioned there are

other ways to do this it's entirely

preference of how you need to deal with

this data it might be easier to return

it as an array if you need to do

something with it in this case we've

looked at fetch object and in the next

video we'll be looking at how we can

fetch into a class