Advertisement
Guest User

Untitled

a guest
May 12th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. class Connect
  4. {
  5.  
  6. private $host = "127.0.0.1"; //localhost
  7. private $database = "academia_hello_php";
  8. private $username = "root";
  9. private $password = "root";
  10. private $driver = "mysql";
  11.  
  12. private $pdo;
  13.  
  14. public function __construct()
  15. {
  16. try {
  17. $this->pdo = new PDO(
  18. "{$this->driver}:host={$this->host};{$this->database}",
  19. $this->username,
  20. $this->password,
  21. array(
  22. PDO::ATTR_PERSISTENT => true //não é fechada no final do script, e sim armazenada em cache
  23. )
  24. );
  25. $this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); //retorna como array associativo
  26. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Além de armazenar o código de erro, este tipo de manipulação de erro irá lançar uma exceção PDOException
  27. } catch (PDOException $e) {
  28. die($e->getMessage());
  29. }
  30. }
  31.  
  32. public function select()
  33. {
  34. $stmt = $this->pdo->query('SELECT * FROM academia_hello_php.alunos');
  35. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  36.  
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement