Guest User

Untitled

a guest
Jan 11th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <?php
  2.  
  3. class Database{
  4.  
  5. public $isConn;
  6. protected $datab;
  7.  
  8. //CONNECT TO DB
  9. public function __construct($host = "", $user = "", $pass = "", $dbname = "", $options = [])
  10. {
  11. $this->isConn = TRUE;
  12. try {
  13. $this->datab = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass, $options);
  14. $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15. $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  16. } catch (PDOException $e){
  17. throw new Exception($e->getMessage());
  18. }
  19. }
  20.  
  21. //DISCONNECT FROM DB
  22. public function Disconnect()
  23. {
  24. $this->isConn = FALSE;
  25. $this->datab = NULL;
  26. }
  27.  
  28. //GET ROW
  29. public function getRow($query, $params = [])
  30. {
  31. try {
  32. $stmt = $this->datab->prepare($query);
  33. $stmt->execute($params);
  34. return $stmt->fetch();
  35. } catch(PDOException $e){
  36. throw new Exception($e->getMessage());
  37. }
  38. }
  39.  
  40. //GET ROWS
  41. public function getRows($query, $params = [])
  42. {
  43. try {
  44. $stmt = $this->datab->prepare($query);
  45. $stmt->execute($params);
  46. return $stmt->fetchAll();
  47. } catch(PDOException $e) {
  48. throw new Exception($e->getMessage());
  49. }
  50. }
  51.  
  52. //INSERT ROW
  53. public function insertRow($query, $params = [])
  54. {
  55. try {
  56. $stmt = $this->datab->prepare($query);
  57. $stmt->execute($params);
  58. return TRUE;
  59. } catch(PDOException $e) {
  60. throw new Exception($e->Message());
  61. }
  62. }
  63.  
  64. //UPDATE ROW
  65. public function updateRow($query, $params = [])
  66. {
  67. $this->insertRow($query, $params);
  68. }
  69.  
  70. //DELETE ROW
  71. public function deleteRow($query, $params = [])
  72. {
  73. $this->insertRow($query, $params);
  74. }
  75.  
  76. public function findAll($table, $params = [])
  77. {
  78. try {
  79. $stmt = $this->datab->prepare("SELECT * FROM $table");
  80. $stmt->execute($params);
  81. return $stmt->fetchAll();
  82. } catch(PDOException $e) {
  83. throw new Exception($e->getMessage());
  84. }
  85. }
  86.  
  87. public function findOne($table, $where, $params = [])
  88. {
  89. try {
  90. $stmt = $this->datab->prepare("SELECT * FROM $table WHERE $where = ? LIMIT 1");
  91. $stmt->execute($params);
  92. return $stmt->fetch();
  93. } catch(PDOException $e) {
  94. throw new Exception($e->getMessage());
  95. }
  96. }
  97.  
  98. public function search($table, $where, $params = [])
  99. {
  100. try {
  101. $stmt = $this->datab->prepare("SELECT * FROM $table WHERE $where LIKE ?");
  102. $stmt->execute($params);
  103. return $stmt->fetchAll(PDO::FETCH_OBJ);
  104. } catch(PDOException $e) {
  105. throw new Exception($e->getMessage());
  106. }
  107. }
  108. }
  109.  
  110. ?>
Add Comment
Please, Sign In to add comment