Guest User

Untitled

a guest
Mar 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4. private $db_host = "";
  5. private $db_user = "";
  6. private $db_pass = "";
  7. private $db_name = null;
  8. private $DBH = null;
  9. private $select = null;
  10. private $insert = null;
  11. private $update = null;
  12. private $error;
  13.  
  14. public function __construct($db_name, $db_host, $db_user, $db_pass, ErrorLog $error){
  15. $this->db_name = $db_name;
  16. $this->db_host = $db_host;
  17. $this->db_user = $db_user;
  18. $this->db_pass = $db_pass;
  19. $this->error = $error;
  20. $this->connect();
  21. }
  22.  
  23. public function connect(){
  24. try {
  25. $this->DBH = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_name . ';port=3306', $this->db_user, $this->db_pass);
  26. $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27. } catch(PDOException $e) {
  28. error_log("Error connecting to database with message: " . $e->getMessage(), 0);
  29. }
  30. }
  31.  
  32. public function disconnect(){
  33. $this->DBH = null;
  34. }
  35.  
  36. public function select($db_query){
  37. $this->select = $this->DBH->query($db_query);
  38. $this->select->setFetchMode(PDO::FETCH_ASSOC);
  39.  
  40. return json_encode($this->getRows());
  41. }
  42.  
  43. public function selectWithParams($db_query, $params){
  44. $this->select = $this->DBH->prepare($db_query);
  45. for($i = 1; $i < sizeof($params) + 1; $i++){
  46. $this->select->bindParam($i, $params[$i - 1]);
  47. }
  48. if($this->select->execute()){
  49. return json_encode($this->getRows());
  50. }
  51. }
  52.  
  53. public function getRows(){
  54. $return = $this->select->fetchAll(PDO::FETCH_ASSOC);
  55. $this->select = null;
  56. return $return;
  57. }
  58.  
  59. public function insert($db_query, $params){
  60. $this->insert = $this->DBH->prepare($db_query);
  61. for($i = 1; $i < sizeof($params) + 1; $i++){
  62. $this->insert->bindParam($i, $params[$i - 1]);
  63. }
  64.  
  65. if($this->insert->execute()){
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71.  
  72. public function insertGetID($db_query, $params){
  73. $this->insert = $this->DBH->prepare($db_query);
  74. for($i = 1; $i < sizeof($params) + 1; $i++){
  75. $this->insert->bindParam($i, $params[$i - 1]);
  76. }
  77.  
  78. if($this->insert->execute()) {
  79. return $this->DBH->lastInsertId();
  80. }
  81. }
  82.  
  83. public function update($db_query, $params){
  84. $this->update = $this->DBH->prepare($db_query);
  85. for($i = 1; $i < sizeof($params) + 1; $i++){
  86. $this->update->bindParam($i, $params[$i - 1]);
  87. }
  88.  
  89. if($this->update->execute()){
  90. $this->update = null;
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96.  
  97. public function delete($db_query){
  98. if($this->DBH->query($db_query)){
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104.  
  105. public function interpolateQuery($query, $params) {
  106. $keys = array();
  107. foreach ($params as $key => $value) {
  108. if (is_string($key)) {
  109. $keys[] = '/:'.$key.'/';
  110. } else {
  111. $keys[] = '/[?]/';
  112. }
  113. }
  114. $query = preg_replace($keys, $params, $query, 1, $count);
  115. error_log($query, 0);
  116. }
  117.  
  118. public function setHost($host){
  119. $this->db_host = $host;
  120. }
  121.  
  122. public function setUser($user){
  123. $this->db_user = $user;
  124. }
  125.  
  126. public function setPass($pass){
  127. $this->db_pass = $pass;
  128. }
  129.  
  130. public function setName($name){
  131. $this->db_name = $name;
  132. }
  133.  
  134. public function setAll($host, $user, $pass, $name){
  135. $this->db_host = $host;
  136. $this->db_user = $user;
  137. $this->db_pass = $pass;
  138. $this->db_name = $name;
  139. }
  140.  
  141. public function getError(){
  142. return $this->DBH->errorInfo();
  143. }
  144. }
Add Comment
Please, Sign In to add comment