Advertisement
jimlei

Untitled

Jun 9th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. // db.php
  2.  
  3. $host = '127.0.0.1';
  4. $db   = 'test';
  5. $user = 'root';
  6. $pass = '';
  7. $charset = 'utf8';
  8.  
  9. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  10. $opt = [
  11.     PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  12.     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  13.     PDO::ATTR_EMULATE_PREPARES   => false,
  14. ];
  15. $pdo = new PDO($dsn, $user, $pass, $opt);
  16.  
  17.  
  18. // controller.php
  19.  
  20. require_once 'db.php'
  21.  
  22. $userModel = new UserModel($db);
  23. $user = $userModel->findOne($_GET['id']);
  24.  
  25.  
  26. // UserModel.php
  27.  
  28. class UserModel
  29. {
  30.  
  31.     private $db;
  32.  
  33.     public function __construct(PDO $db)
  34.     {
  35.          $this->db = $db;
  36.     }
  37.  
  38.     public function findOne($id)
  39.     {
  40.         $stmt = $this->db->prepare("SELECT FROM user WHERE id = ?");
  41.         $stmt->execute([$id]);
  42.         return $stmt->fetch();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement