020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO::FETCH_INTO
[MySQL PDO Const]

PDO::FETCH_INTO (integer)

Specifies that the fetch method shall update an existing instance of the requested class, mapping the columns to named properties in the class.

Used with:

  • PDO::query  [more]
  • PDOStatement::setFetchMode  [more]
  • PDOStatement:: fetch  [more]
  • PDOStatement:: fetchAll  [more]

 

 

PDO::query

note: you'd be better off using PDPStatement::prepare/execute than this function - especially for repetitive queries

Here is function construction:

PDO::query ( string $statement , int $PDO::FETCH_INTO , object $object )

Lets take a look at them.

string $statement: e.g. "SELECT entry_title, entry_body, status FROM posts"
int $PDO::FETCH_CLASS: specifies to fetch that statement into selected class / ino other words, to create a new instance od that class.
object $object: this is a selected object name, that class must exists

code:

class myClass {
    function __construct() {
    }
}

$sql = "SELECT entry_title, entry_body, status FROM posts";
$stmt = $dbh->query($sql, PDO::FETCH_INTO, new myClass());
$result = $stmt->fetch();

print_r($result);
var_dump($result);

 

output:

print_r:

myClass Object
(
    [entry_title] => post 1
    [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [status] => 0
)
 
var dump:
 
object(myClass)[2]
     public 'entry_title' => string 'post 1' (length=6)
     public 'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
     public 'status' => string '0' (length=1)
 

 

 

 

PDOStatement::setFetchMode

PDOStatement::fetch

PDOStatement::fetchAll (to get this example, just replace fetch() with fetchAll())

code:

class myClass {
    function __construct() {
    }
}

$sql = "SELECT entry_title, entry_body, status FROM posts";
$stmt = $dbh->query($sql);
$stmt->setFetchMode(PDO::FETCH_INTO, new myClass());
$result = $stmt->fetch();

print_r($result);
var_dump($result);

output:

print_r:

myClass Object
(
    [entry_title] => post 1
    [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [status] => 0
)

var_dump:

object(myClass)[3]
     public 'entry_title' => string 'post 1' (length=6)
     public 'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
     public 'status' => string '0' (length=1)