Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO fetching into objects

Tags:

php

pdo

I am trying to figure out how to fetch from PDO into my custom class, and in general the PDO-to-object API, and am finding the lack of decent documentation frustrating. Most of the options are only documented as an option, while the examples all use fetching into arrays.
So, can someone explain how these are used:

  • PDO::FETCH_OBJ
  • PDO::FETCH_CLASS
  • PDO::FETCH_CLASSTYPE
  • PDO::FETCH_INTO
  • PDO::FETCH_LAZY
  • PDOStatement::fetch
  • PDOStatement::fetchObject
  • PDOStatement::setFetchMode

If possible, I would like a general explanation how each function/constant is used for fetching objects, or what the differences are, as well as a specific answer to how do I fetch into my class, e.g. into:

class MyRow {
  public $col1, $col2;
}
like image 496
Baruch Avatar asked Oct 14 '12 11:10

Baruch


People also ask

How to fetch data from PDO in PHP?

Fetch data from a result set by calling one of the following fetch methods: To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method.

What does PDO fetch do?

Parameters ¶ PDO::FETCH_CLASS : returns a new instance of the requested class, mapping the columns of the result set to named properties in the class, and calling the constructor afterwards, unless PDO::FETCH_PROPS_LATE is also given.

How to get data from database PDO?

First, connect to a MySQL database. Check it out the connecting to MySQL database using PDO tutorial for detail information. Then, construct a SELECT statement and execute it by using the query() method of the PDO object. The query() method of the PDO object returns a PDOStatement object, or false on failure.

Which of the following methods are used to fetch data from a PDO statement?

To fetch results in PDO, you have the option of $stmt->fetch() or $stmt->fetchAll() . The former is more versatile, as it can be used to fetch one row, or all if used in a loop.


2 Answers

Here is what I managed to figure out:

Constants

  • PDO::FETCH_OBJ
    Is used to fetch into a new instance of an unnamed ("anonymous") object

  • PDO::FETCH_CLASS
    Is used to fetch into a new instance of an existing class (the column names should match existing properties, or __set should be used to accept all properties). The constructor of the class will be called after the properties are set.

  • PDO::FETCH_CLASSTYPE
    Used with FETCH_CLASS (bitwise OR), the name of the class to create an instance of is in the first column, instead of supplied to the function.

  • PDO::FETCH_INTO
    Is used with the same type of class as FETCH_CLASS (must handle all columns as property names), but updates an existing object as opposed to creating a new one.

  • PDO::FETCH_LAZY
    I don't know what this does.

Functions

  • PDOStatement::fetch
    The regular get-a-row command. I don't know how to use this with FETCH_CLASS or FETCH_INTO since there does not be any way to pass the class name/instance.

  • PDOStatement::fetchObject
    A way to do FETCH_CLASS or FETCH_INTO, including passing constructor args. With no args is a shorthand for FETCH_OBJ.

  • PDOStatement::setFetchMode
    A way to set the default fetch mode, so that PDOStatment::fetch can be called without args.


That is the best I managed to figure out. I hope it helps someone else (I needed the fetchObject method)

like image 81
Baruch Avatar answered Nov 06 '22 02:11

Baruch


After preparing a statement, use PDOStatement::setFetchMode with PDO::FETCH_CLASS:

$stmt = $pdo->query('SELECT col1, col2 FROM table');
$stmt->setFetchMode(\PDO::FETCH_CLASS, 'MyRow');
$obj = $stmt->fetch();
like image 43
Brendan Smith Avatar answered Nov 06 '22 02:11

Brendan Smith