-
-
Save jlpoveda/a6331501ab669797dd3c to your computer and use it in GitHub Desktop.
PDO::FETCH_CLASS sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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