Guest User

Untitled

a guest
Oct 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.62 KB | None | 0 0
  1. class Student {
  2.  
  3.     public $id;
  4.     public $first_name;
  5.     public $last_name
  6.  
  7.     public function getFullName() {
  8.         return $this->first_name.' '.$this->last_name
  9.     }
  10. }
  11.  
  12. try
  13. {
  14.     $dbh = new PDO("mysql:host=$hostname;dbname=school", $username, $password)
  15.  
  16.     $stmt = $dbh->query("SELECT * FROM students");
  17.  
  18.     /* MAGIC HAPPENS HERE */
  19.  
  20.     $stmt->setFetchMode(PDO::FETCH_INTO, new Student);
  21.  
  22.  
  23.     foreach($stmt as $student)
  24.     {
  25.         echo $student->getFullName().'<br />';
  26.     }
  27.  
  28.     $dbh = null;
  29. }
  30. catch(PDOException $e)
  31. {
  32.     echo $e->getMessage();
  33. }
  34.  
  35. //Dit is het grote nut van PDO..
Add Comment
Please, Sign In to add comment