PDO - setFetchMode() before each getting?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • webcm123@gmail.com

    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(PD O::FETCH_ASSOC) before each
    Foreach.

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

    Or:
    PDO->setAttribute(P DO::FETCH, PDO::FETCH_ASSO C);

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

  • ZeldorBlat

    #2
    Re: PDO - setFetchMode() before each getting?

    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(PD O::FETCH_ASSOC) before each
    Foreach.
    >
    Simply - lack of PDO->defaultFetchMo de(). 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(P DO::FETCH, PDO::FETCH_ASSO C);
    >
    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.

    Comment

    • Michael Fesser

      #3
      Re: PDO - setFetchMode() before each getting?

      ..oO(webcm123@g mail.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(PD O::FETCH_ASSOC) before each
      >Foreach.
      Nope.
      >Simply - lack of PDO->defaultFetchMo de(). 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(P DO::FETCH, PDO::FETCH_ASSO C);
      If you still want to have a defaultFetchMod e() 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

      Comment

      Working...