Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. <?php
  2. class Database {
  3. private $host = DB_HOST;
  4. private $user = DB_USER;
  5. private $pass = DB_PASS;
  6. private $dbname = DB_NAME;
  7.  
  8. private $dbh;
  9. private $error;
  10. private $stmt;
  11.  
  12. public function __construct() {
  13. // Set DSN
  14. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  15. // Set options
  16. $options = array (
  17. PDO::ATTR_PERSISTENT => false,
  18. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  19. );
  20. // Create a new PDO instanace
  21. try {
  22. $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
  23. } // Catch any errors
  24. catch ( PDOException $e ) {
  25. $this->error = $e->getMessage();
  26. }
  27. }
  28.  
  29.  
  30. public function query($query) {
  31. $this->stmt = $this->dbh->prepare($query);
  32. }
  33.  
  34.  
  35. public function bind($param, $value, $type = null) {
  36. if (is_null ( $type )) {
  37. switch (true) {
  38. case is_int ( $value ) :
  39. $type = PDO::PARAM_INT;
  40. break;
  41. case is_bool ( $value ) :
  42. $type = PDO::PARAM_BOOL;
  43. break;
  44. case is_null ( $value ) :
  45. $type = PDO::PARAM_NULL;
  46. break;
  47. default :
  48. $type = PDO::PARAM_STR;
  49. }
  50. }
  51. $this->stmt->bindValue ( $param, $value, $type );
  52. }
  53.  
  54.  
  55. public function execute(){
  56. return $this->stmt->execute();
  57. }
  58.  
  59.  
  60. public function resultset(){
  61. $this->execute();
  62. return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  63. }
  64.  
  65.  
  66. public function single(){
  67. $this->execute();
  68. return $this->stmt->fetch(PDO::FETCH_OBJ);
  69. }
  70.  
  71.  
  72. public function rowCount(){
  73. return $this->stmt->rowCount();
  74. }
  75.  
  76.  
  77. public function lastInsertId(){
  78. return $this->dbh->lastInsertId();
  79. }
  80.  
  81.  
  82. public function beginTransaction(){
  83. return $this->dbh->beginTransaction();
  84. }
  85.  
  86.  
  87. public function endTransaction(){
  88. return $this->dbh->commit();
  89. }
  90.  
  91.  
  92. public function cancelTransaction(){
  93. return $this->dbh->rollBack();
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement