Guest User

Untitled

a guest
Jun 23rd, 2018
78
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. class db{
  4. public $mysqli;
  5. private $total_queries = 0;
  6. protected $result = false;
  7.  
  8.  
  9. public function __construct($type){
  10.  
  11.  
  12.  
  13. if ($this->mysqli->connect_error){
  14. exit('Unable to establish a connection to your database. Please verify your settings are correct.');
  15. }
  16.  
  17. }
  18.  
  19. public function query($sql, $binds = false){
  20.  
  21. if ($binds){
  22. $sql = $this->_binds($sql, $binds);
  23. }
  24.  
  25. if (($this->result = $this->mysqli->query($sql)) === false){
  26. exit("Invalid query : ".$this->mysqli->error);
  27. return false;
  28. }
  29. return true;
  30. }
  31.  
  32. public function select($select, $table, $where = '', $binds = '', $orderby = '', $order = 'ASC', $limit = 0){
  33.  
  34. $sql = 'SELECT ';
  35.  
  36. if (!is_array($select)){
  37. $select = array($select);
  38. }
  39.  
  40. $i = 0;
  41. foreach ($select as $v){
  42. $sql .= ($i == 0) ? '' : ',';
  43. $sql .= $v;
  44. ++$i;
  45. }
  46.  
  47. $sql .= ' FROM ';
  48. $sql .= $table;
  49.  
  50. if (!empty($where)){
  51. $sql .= ' WHERE '.$where;
  52. }
  53.  
  54. if (!empty($orderby)){
  55. $sql .= ' ORDER BY '.$orderby.' '.$order;
  56. }
  57.  
  58. if ($limit > 0 || !empty($limit)){
  59. $sql .= ' LIMIT '.$limit;
  60.      }
  61.  
  62. return $this->query($sql, $binds);
  63. }
  64.  
  65. public function join($type = '', $select, $tables, $on, $where = '', $binds = '', $orderby = '', $order = 'ASC', $limit = 0){
  66.  
  67. $tableSet = '';
  68. $key = 'a';
  69. foreach ($tables as $table){
  70. $tableSet .= DB_PREFIX.$table.' AS '.$key;
  71. if ($key == 'a'){
  72. $key = 'b';
  73. $tableSet .= ' '.$type.' JOIN ';
  74. }else{
  75. $tableSet .= ' ON '.$on;
  76. }
  77. }
  78. return $this->select($select, $tableSet, $where, $binds, $orderby, $order, $limit);
  79. }
  80.  
  81. public function result($type = 'object'){
  82. return ($type == 'array') ? $this->fetch_array() : $this->fetch_object();
  83. }
  84.  
  85.  
  86. public function insert($table, $args){
  87.  
  88. $keys = '';
  89. $values = '';
  90. $i = count($args);
  91. $x = 0;
  92. foreach ($args as $k => $v){
  93. ++$x;
  94. $comma = ($x < $i) ? ',' : '';
  95. $keys .= $k . $comma;
  96. $values .= $this->cleanse($v) . $comma;
  97. }
  98. $sql = 'INSERT INTO '.DB_PREFIX.$table.'('.$keys.') VALUES('.$values.')';
  99. return $this->query($sql);
  100. }
  101.  
  102.  
  103. public function update($table, $args = array(), $where = array()){
  104.  
  105. $update = '';
  106. $i = count($args);
  107. $x = 0;
  108. foreach ($args as $k => $v){
  109. ++$x;
  110. $comma = ($x < $i) ? ',' : '';
  111. $update .= $k.'=\''.$this->_cleanse($v).'\''.$comma;
  112. }
  113.  
  114. $sql = 'UPDATE '.DB_PREFIX.$table.' SET '.$update.' WHERE';
  115. $and = false;
  116. foreach ($where as $k => $v){
  117. if ($and) $sql .= ' AND';
  118. $sql .= ' '.$k.'=\''.$this->_cleanse($v).'\'';
  119. $and = true;
  120. }
  121. return $this->query($sql);
  122. }
  123.  
  124. public function delete($table, $where, $binds = array()){
  125.  
  126. $sql = 'DELETE FROM '.DB_PREFIX.$table.' WHERE '.$where;
  127. return $this->query($sql, $binds);
  128. }
  129.  
  130. private function fetch_object(){
  131. return $this->result->fetch_object();
  132. }
  133.  
  134.  
  135. private function fetch_array(){
  136.  
  137. while (($result = $this->result->fetch_assoc())){
  138. $array[] = $result;
  139. }
  140. return $array;
  141. }
  142.  
  143. public function num_rows(){
  144. return $this->result->num_rows;
  145. }
  146.  
  147. public function insert_id(){
  148. return $this->mysqli->insert_id;
  149. }
  150.  
  151. private function _binds($sql, $binds){
  152.  
  153. if (!is_array($binds)){
  154. $binds = array($binds);
  155. }
  156.  
  157. $segments = explode('?', $sql);
  158.  
  159. if (count($binds) >= count($segments)){
  160. $binds = array_slice($binds, 0, count($segments) - 1);
  161. }
  162.  
  163. $sql = $segments[0];
  164. $i = 0;
  165. foreach ($binds as $bind){
  166. $sql .= $this->cleanse($bind);
  167. $sql .= $segments[++$i];
  168. }
  169.  
  170. return $sql;
  171. }
  172.  
  173. private function cleanse($str){
  174.  
  175. if (is_string($str)){
  176. $str = "'".$this->_cleanse($str)."'";
  177. }
  178.  
  179. elseif (is_bool($str)){
  180. $str = ($str === false) ? 0 : 1;
  181. }else{
  182. $str = ($str === NULL) ? 'NULL' : $str;
  183. }
  184. return $str;
  185. }
  186.  
  187.  
  188. private function _cleanse($str){
  189.  
  190. if (function_exists('mysqli_real_escape_string')){
  191. $str = $this->mysqli->real_escape_string($str);
  192. }else{
  193. $str = addslashes($str);
  194. }
  195. return $str;
  196. }
  197.  
  198. public function close(){
  199. $this->mysqli->close();
  200. }
  201.  
  202. }
Add Comment
Please, Sign In to add comment