Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 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. //errorhandling variables
  9. private $dbh;
  10. private $error;
  11.  
  12. private $stmt;
  13.  
  14. public function __construct(){
  15. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  16.  
  17. //1=>Check if there's already an open connection
  18. //2=>Return errors as exceptions, which can be handled later on
  19. $options = array(
  20. PDO::ATTR_PERSISTENT => true,
  21. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  22. );
  23.  
  24. //Try to connect
  25. try {
  26. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  27. }
  28. catch (PDOException $e) {
  29. $this->error = $e->getMessage();
  30. }
  31. }
  32.  
  33. public function query($query){
  34. $this->stmt = $this->dbh->prepare($query);
  35. }
  36.  
  37. public function bind($param, $value, $type = null){
  38. if (is_null($type)) {
  39. switch (true) {
  40. case is_int($value):
  41. $type = PDO::PARAM_INT;
  42. break;
  43. case is_bool($value):
  44. $type = PDO::PARAM_BOOL;
  45. break;
  46. case is_null($value):
  47. $type = PDO::PARAM_NULL;
  48. break;
  49. default:
  50. $type = PDO::PARAM_STR;
  51. }
  52. }
  53. $this->stmt->bindValue($param, $value, $type);
  54. }
  55.  
  56. public function execute(){
  57. return $this->stmt->execute();
  58. }
  59.  
  60. public function resultset(){
  61. $this->execute();
  62. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  63. }
  64.  
  65. public function single(){
  66. $this->execute();
  67. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  68. }
  69.  
  70. public function rowCount(){
  71. return $this->stmt->rowCount();
  72. }
  73.  
  74. public function lastInsertId(){
  75. return $this->dbh->lastInsertId();
  76. }
  77.  
  78. public function beginTransaction(){
  79. return $this->dbh->beginTransaction();
  80. }
  81.  
  82. public function endTransaction(){
  83. return $this->dbh->commit();
  84. }
  85.  
  86. public function cancelTransaction(){
  87. return $this->dbh->rollBack();
  88. }
  89.  
  90. public function debugDumpParams(){
  91. return $this->stmt->debugDumpParams();
  92. }
  93. }
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement