468,242 Members | 1,767 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

PDO - setFetchMode() before each getting?

Must I use PDOStatement->setFetchMode() before each fetching data if I
want to get only or usually ASSOC arrays? Perhaps, not.

foreach( $stmt as $array ) { ... }

It's the fastest way for fetching records to $array from result. So I
have to insert $stmt -setFetchMode(PDO::FETCH_ASSOC) before each
Foreach.

Simply - lack of PDO->defaultFetchMode(). No one thought about it
creating PDO. Who can tell PHP or PDO developers about it?

Or:
PDO->setAttribute(PDO::FETCH, PDO::FETCH_ASSOC);

Why does PDO have constants instead of parameters? Typing PDO::[...]
isn't so comfortable and I wonder if it isn't slower.

Jun 17 '07 #1
2 3613
On Jun 17, 12:39 pm, webcm...@gmail.com wrote:
Must I use PDOStatement->setFetchMode() before each fetching data if I
want to get only or usually ASSOC arrays? Perhaps, not.
You could just try it and find out.
>
foreach( $stmt as $array ) { ... }

It's the fastest way for fetching records to $array from result. So I
have to insert $stmt -setFetchMode(PDO::FETCH_ASSOC) before each
Foreach.

Simply - lack of PDO->defaultFetchMode(). No one thought about it
creating PDO. Who can tell PHP or PDO developers about it?
You can. There are plenty of places on the PHP website where you can
request features, report bugs, or just whine about things.
>
Or:
PDO->setAttribute(PDO::FETCH, PDO::FETCH_ASSOC);

Why does PDO have constants instead of parameters? Typing PDO::[...]
isn't so comfortable and I wonder if it isn't slower.
So that PDO constants don't clash with other constants defined in the
global scope. Just because it's uncomfortable for you to type
doesn't make it wrong or incorrect. And I doubt there is any
practical difference in access speed.

Jun 17 '07 #2
..oO(we******@gmail.com)
>Must I use PDOStatement->setFetchMode() before each fetching data if I
want to get only or usually ASSOC arrays? Perhaps, not.

foreach( $stmt as $array ) { ... }

It's the fastest way for fetching records to $array from result. So I
have to insert $stmt -setFetchMode(PDO::FETCH_ASSOC) before each
Foreach.
Nope.
>Simply - lack of PDO->defaultFetchMode(). No one thought about it
creating PDO. Who can tell PHP or PDO developers about it?
There's no need for such a function, because of:
>Or:
PDO->setAttribute(PDO::FETCH, PDO::FETCH_ASSOC);
If you still want to have a defaultFetchMode() method you can always
write your own. Just derive a child class from PDO and implement that
method. But it's not really necessary.
>Why does PDO have constants instead of parameters?
Using constants provides flexibility, reliability and portability. You
don't have to care about the real internal (often numeric) value, since
you just use the descriptive and more readable constant.
>Typing PDO::[...]
isn't so comfortable and I wonder if it isn't slower.
You have to use the PDO:: prefix, because the constants are defined
inside the PDO class. Since PHP doesn't support namespaces, this is
quite a good idea to avoid name conflicts in the global namespace.

If I want to use similar names in one of my own classes for whatever
reason, I can do that without any problems:

class TExample {
const FETCH_ASSOC = 1;
const FETCH_NUM = 2;
...
}

Same name, but different namespace.

Micha
Jun 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Floris van Haaster | last post: by
13 posts views Thread by tshad | last post: by
8 posts views Thread by Candace | last post: by
2 posts views Thread by dkmd_nielsen | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.