Advertisement
Guest User

Untitled

a guest
Sep 6th, 2016
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. class DB {
  21. private static $_instance = null;
  22. private $_pdo, $_query, $_error = false, $_results, $_resultsArray, $_count = 0, $_lastId;
  23.  
  24. private function __construct(){
  25. try{
  26. $this->_pdo = new PDO('sqlsrv:server=' .
  27. Config::get('mysql/host') .';Database='.
  28. Config::get('mysql/db'),
  29. Config::get('mysql/username'),
  30. Config::get('mysql/password'),
  31. array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode = ''"));
  32. } catch(PDOException $e){
  33. die($e->getMessage());
  34. }
  35. }
  36.  
  37. public static function getInstance(){
  38. if (!isset(self::$_instance)) {
  39. self::$_instance = new DB();
  40. }
  41. return self::$_instance;
  42. }
  43.  
  44. public function query($sql, $params = array()){
  45. $this->_error = false;
  46. if ($this->_query = $this->_pdo->prepare($sql)) {
  47. $x = 1;
  48. if (count($params)) {
  49. foreach ($params as $param) {
  50. $this->_query->bindValue($x, $param);
  51. $x++;
  52. }
  53. }
  54.  
  55. if ($this->_query->execute()) {
  56. $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
  57. $this->_resultsArray = json_decode(json_encode($this->_results),true);
  58. $this->_count = $this->_query->rowCount();
  59. $this->_lastId = $this->_pdo->lastInsertId();
  60. } else{
  61. $this->_error = true;
  62. }
  63. }
  64. return $this;
  65. }
  66.  
  67. public function findAll($table){
  68. return $this->action('SELECT *',$table);
  69. }
  70.  
  71. public function findById($id,$table){
  72. return $this->action('SELECT *',$table,array('id','=',$id));
  73. }
  74.  
  75. public function action($action, $table, $where = array()){
  76. $sql = "{$action} FROM {$table}";
  77. $value = '';
  78. if (count($where) === 3) {
  79. $operators = array('=', '>', '<', '>=', '<=');
  80.  
  81. $field = $where[0];
  82. $operator = $where[1];
  83. $value = $where[2];
  84.  
  85. if(in_array($operator, $operators)){
  86. $sql .= " WHERE {$field} {$operator} ?";
  87. }
  88. }
  89. if (!$this->query($sql, array($value))->error()) {
  90. return $this;
  91. }
  92. return false;
  93. }
  94.  
  95. public function get($table, $where){
  96. return $this->action('SELECT *', $table, $where);
  97. }
  98.  
  99. public function delete($table, $where){
  100. return $this->action('DELETE', $table, $where);
  101. }
  102.  
  103. public function deleteById($table,$id){
  104. return $this->action('DELETE',$table,array('id','=',$id));
  105. }
  106.  
  107. public function insert($table, $fields = array()){
  108. $keys = array_keys($fields);
  109. $values = null;
  110. $x = 1;
  111.  
  112. foreach ($fields as $field) {
  113. $values .= "?";
  114. if ($x < count($fields)) {
  115. $values .= ', ';
  116. }
  117. $x++;
  118. }
  119.  
  120. $sql = "INSERT INTO {$table} (`". implode('`,`', $keys)."`) VALUES ({$values})";
  121.  
  122. if (!$this->query($sql, $fields)->error()) {
  123. return true;
  124. }
  125. return false;
  126. }
  127.  
  128. public function update($table, $id, $fields){
  129. $set = '';
  130. $x = 1;
  131.  
  132. foreach ($fields as $name => $value) {
  133. $set .= "{$name} = ?";
  134. if ($x < count($fields)) {
  135. $set .= ', ';
  136. }
  137. $x++;
  138. }
  139.  
  140. $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  141.  
  142. if (!$this->query($sql, $fields)->error()) {
  143. return true;
  144. }
  145. return false;
  146. }
  147.  
  148. public function results($assoc = false){
  149. if($assoc) return $this->_resultsArray;
  150. return $this->_results;
  151. }
  152.  
  153. public function first(){
  154. return $this->results()[0];
  155. }
  156.  
  157. public function count(){
  158. return $this->_count;
  159. }
  160.  
  161. public function error(){
  162. return $this->_error;
  163. }
  164.  
  165. public function lastId(){
  166. return $this->_lastId;
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement