Home > PDOStatement->fetch() > The PDO fetch flags are actually constants which represent integers

The PDO fetch flags are actually constants which represent integers


PDO Fetch Flags – The Constants Represent Integers

If you look at the prototype of the PDOStatement->fetch() function,
you can see that it only accepts integers as its input

PDOStatement::fetch  ([ int $fetch_style = PDO::FETCH_BOTH  [, int $cursor_orientation = PDO::FETCH_ORI_NEXT  [, int $cursor_offset = 0  ]]] )

For example, when you use PDOStatement::fetch(PDO::FETCH_ASSOC), you are putting in a constant PDO:FETCH_ASSOC
and that constant represents the integer 2.

Therefore $PDOStatement->fetch(2) is equivilent to $PDOStatement->fetch(PDO::FETCH_ASSOC);
Try it, it works.

I can think of no sane reason why you would ever want to use the integer over the constant.
If you did, your code would be very hard to read. But as a simple curiosity, here are
the integer values of the PDO fetch flags.

PDO::FETCH_ASSOC = 2
PDO::FETCH_BOTH = 4
PDO::FETCH_BOUND = 6
PDO::FETCH_CLASS = 8
PDO::FETCH_INTO = 9
PDO::FETCH_LAZY = 1
PDO::FETCH_NUM = 3
PDO::FETCH_OBJ = 5

<?php
 echo    PDO::FETCH_ASSOC."\n";// 2
 echo    PDO::FETCH_BOTH."\n";// 4
 echo    PDO::FETCH_BOUND."\n";// 6
 echo    PDO::FETCH_CLASS."\n";// 8
 echo    PDO::FETCH_INTO."\n";// 9
 echo    PDO::FETCH_LAZY."\n"; //1
 echo    PDO::FETCH_NUM."\n"; //3
 echo    PDO::FETCH_OBJ."\n"; //5
?>
Advertisements
Categories: PDOStatement->fetch()
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: