Advertisement
Guest User

Untitled

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