Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /*
  2. config.php
  3. define("DB_HOST", "localhost");
  4. define("DB_USER", "USER");
  5. define("DB_PASS", "PASSWORD");
  6. define("DB_NAME", "DBNAME");
  7. */
  8.  
  9. class Database {
  10. private $host = DB_HOST;
  11. private $user = DB_USER;
  12. private $pass = DB_PASS;
  13. private $dbname = DB_NAME;
  14. private $db;
  15. private $error;
  16. private $stmt;
  17.  
  18. public function __construct() {
  19. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8';
  20. $options = array(
  21. PDO::ATTR_PERSISTENT => true,
  22. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  23. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
  24. PDO::ATTR_EMULATE_PREPARES => false
  25. );
  26. try {
  27. $this->db = new PDO($dsn, $this->user, $this->pass, $options);
  28. } catch (PDOException $e) {
  29. $this->error = $e->getMessage();
  30. }
  31. }
  32.  
  33. public function query($query) {
  34. $this->stmt = $this->db->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_OBJ);
  63. }
  64.  
  65. public function resultsetA() {
  66. $this->execute();
  67. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  68. }
  69.  
  70. public function column($number) {
  71. $this->execute();
  72. return $this->stmt->fetchColumn($number);
  73. }
  74.  
  75. public function single() {
  76. $this->execute();
  77. return $this->stmt->fetch(PDO::FETCH_OBJ);
  78. }
  79.  
  80. public function rowCount() {
  81. return $this->stmt->rowCount();
  82. }
  83.  
  84. public function lastInsertId() {
  85. return $this->db->lastInsertId();
  86. }
  87.  
  88. public function beginTransaction() {
  89. return $this->db->beginTransaction();
  90. }
  91.  
  92. public function endTransaction() {
  93. return $this->db->commit();
  94. }
  95.  
  96. public function cancelTransaction() {
  97. return $this->db->rollBack();
  98. }
  99.  
  100. public function debugDumpParams() {
  101. return $this->stmt->debugDumpParams();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement