Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2. class dbSchemaPg
  3. {
  4. public $result;
  5. private $stm, $dbh;
  6. private $host = DB_SERVER;
  7. private $db_name = DB_NAME;
  8. private $username = DB_USER;
  9. private $password = DB_PASS;
  10. private $port = DB_PORT;
  11.  
  12. function __construct()
  13. {
  14. $this->connectToDB();
  15. }
  16.  
  17. private function connectToDB()
  18. {
  19. try {
  20. $this->dbh = new PDO("pgsql:host={$this->host};port={$this->port};dbname={$this->db_name};user={$this->username};password={$this->password}");
  21. $this->dbh->exec("set names utf8");
  22. }
  23. catch (PDOException $e) {
  24. echo $e->getMessage();
  25. }
  26. return ($this->dbh);
  27. }
  28.  
  29. public function query($sql)
  30. {
  31. try {
  32. $this->result = $this->dbh->exec($sql) or die("QUERY FAILED !!! " . $sql);
  33. return $this->result;
  34. }
  35. catch (PDOException $e) {
  36. echo $e->getMessage();
  37. }
  38. }
  39.  
  40. public function last_insert_id()
  41. {
  42. try {
  43. $this->result = $this->dbh->lastInsertId() or die("QUERY FAILED !!! " . $sql);
  44. return $this->result;
  45. }
  46. catch (PDOException $e) {
  47. echo $e->getMessage();
  48. }
  49. }
  50. public function fetchQuery($sql)
  51. {
  52. try {
  53. $this->stm = $this->dbh->query($sql) or die("QUERY FAILED !!! " . $sql);
  54. $this->result = $this->stm->fetchAll(PDO::FETCH_ASSOC);
  55. return $this->result;
  56. }
  57. catch (PDOException $e) {
  58. echo $e->getMessage();
  59. }
  60. }
  61.  
  62. public function queryPrepared($sql, $vars)
  63. {
  64. try {
  65. $this->result = $this->dbh->prepare($sql) or die("QUERY FAILED !!! " . $sql);
  66. $this->result->execute($vars);
  67. // only for returning value #TODO check for rowcount();
  68. $this->result = $this->result->fetchAll(PDO::FETCH_ASSOC);
  69. return $this->result;
  70. }
  71. catch (PDOException $e) {
  72. echo $e->getMessage();
  73. }
  74. }
  75.  
  76. public function fetchQueryPrepared($sql, $vars)
  77. {
  78. try {
  79. $this->stm = $this->dbh->prepare($sql) or die("QUERY FAILED !!! " . $sql);
  80. $this->stm->execute($vars);
  81. $this->result = $this->stm->fetchAll(PDO::FETCH_ASSOC);
  82.  
  83. return $this->result;
  84. $this->dbh = null;
  85. }
  86. catch (PDOException $e) {
  87. echo $e->getMessage();
  88. }
  89. }
  90. }
  91. $db_pg = new dbSchemaPg();
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement