Skip to content

Instantly share code, notes, and snippets.

@jlpoveda
Created August 24, 2015 21:02
Show Gist options
  • Save jlpoveda/a6331501ab669797dd3c to your computer and use it in GitHub Desktop.
Save jlpoveda/a6331501ab669797dd3c to your computer and use it in GitHub Desktop.
PDO::FETCH_CLASS sample
<?php
class User {
private $matricula;
private $marca_id;
public function __construct($matricula, $marca_id)
{
$this->matricula = $matricula;
$this->marca_id = $marca_id;
}
public function display() {
echo $this->matricula . ' -- ' . $this->marca_id . PHP_EOL;
}
}
$dns = 'mysql:host=localhost;dbname=pruebas';
$user = 'root';
$pass = '';
$db = new PDO($dns, $user, $pass);
$sql = "SELECT matricula, marca_id FROM stock_tienda";
$sq = $db->query($sql);
foreach ($sq->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'user', array(null, null)) as $r) {
$r->display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment