Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. define('DB','sistema_postgres');
  2. define('HOST','localhost');
  3. define('USER','postgres');
  4. define('PASS','123');
  5.  
  6. require_once 'config.php';
  7. class Database {
  8. private $db;
  9. private $host;
  10. private $user;
  11. private $pass;
  12. private $conn;
  13.  
  14. public function __construct(){
  15. $this->host = HOST;
  16. $this->db = DB;
  17. $this->user = USER;
  18. $this->pass = PASS;
  19. $this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};user={$this->user};password={$this->pass}");
  20. }
  21.  
  22. protected function getConn(){
  23. return $this->conn;
  24. }
  25. }
  26.  
  27. require_once('database.php');
  28. class TurmaDAO extends Database implements IDAO{
  29. private $turma;
  30. private $db;
  31.  
  32. public function __construct($turma=null) {
  33. if(isset($turma)){
  34. $this->setTurma($turma);
  35. }
  36. parent::__construct();
  37. $this->db = parent::getConn();
  38. }
  39.  
  40. public function listAll() {
  41. $stmt = $this->db->prepare('SELECT * FROM turma');
  42. $stmt->execute();
  43. try{
  44. $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  45. }
  46. catch(PDOException $e){
  47. echo $e->getMessage();
  48. die();
  49. }
  50. return $rs;
  51. }
  52. }
  53.  
  54. require_once 'TurmaDAO.php';
  55. $t = new TurmaDAO();
  56. $rs = $t->listAll();
  57. var_dump($rs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement