Guest User

Untitled

a guest
Jul 8th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. <?php
  2. if ( !class_exists( 'DB' ) ) {
  3. class DB {
  4. public function __construct($user, $password, $database, $host = 'localhost') {
  5. $this->user = $user;
  6. $this->password = $password;
  7. $this->database = $database;
  8. $this->host = $host;
  9. }
  10. protected function connect() {
  11. return new mysqli($this->host, $this->user, $this->password, $this->database);
  12. }
  13. public function query($query) {
  14. $db = $this->connect();
  15. $result = $db->query($query);
  16.  
  17. while ( $row = $result->fetch_object() ) {
  18. $results[] = $row;
  19. }
  20.  
  21. return $results;
  22. }
  23. public function insert($table, $data, $format) {
  24. // Check for $table or $data not set
  25. if ( empty( $table ) || empty( $data ) ) {
  26. return false;
  27. }
  28.  
  29. // Connect to the database
  30. $db = $this->connect();
  31.  
  32. // Cast $data and $format to arrays
  33. $data = (array) $data;
  34. $format = (array) $format;
  35.  
  36. // Build format string
  37. $format = implode('', $format);
  38. $format = str_replace('%', '', $format);
  39.  
  40. list( $fields, $placeholders, $values ) = $this->prep_query($data);
  41.  
  42. // Prepend $format onto $values
  43. array_unshift($values, $format);
  44.  
  45. // Prepary our query for binding
  46. $stmt = $db->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");
  47.  
  48. // Dynamically bind values
  49. call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
  50.  
  51. // Execute the query
  52. $stmt->execute();
  53.  
  54. // Check for successful insertion
  55. if ( $stmt->affected_rows ) {
  56. return true;
  57. }
  58.  
  59. return false;
  60. }
  61.  
  62.  
  63. public function update($table, $data, $format, $where, $where_format) {
  64. // Check for $table or $data not set
  65. if ( empty( $table ) || empty( $data ) ) {
  66. return false;
  67. }
  68.  
  69. // Connect to the database
  70. $db = $this->connect();
  71.  
  72. // Cast $data and $format to arrays
  73. $data = (array) $data;
  74. $format = (array) $format;
  75.  
  76. // Build format array
  77. $format = implode('', $format);
  78. $format = str_replace('%', '', $format);
  79. $where_format = implode('', $where_format);
  80. $where_format = str_replace('%', '', $where_format);
  81. $format .= $where_format;
  82.  
  83. list( $fields, $placeholders, $values ) = $this->prep_query($data, 'update');
  84.  
  85. //Format where clause
  86. $where_clause = '';
  87. //$where_values = '';
  88. $where_values = null;
  89. $count = 0;
  90.  
  91. foreach ( $where as $field => $value ) {
  92. if ( $count > 0 ) {
  93. $where_clause .= ' AND ';
  94. }
  95.  
  96. $where_clause .= $field . '=?';
  97. $where_values[] = $value;
  98.  
  99. $count++;
  100. }
  101.  
  102. // Prepend $format onto $values
  103. array_unshift($values, $format);
  104. $values = array_merge($values, $where_values);
  105.  
  106. // Prepary our query for binding
  107. $stmt = $db->prepare("UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");
  108.  
  109. // Dynamically bind values
  110. call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
  111.  
  112. // Execute the query
  113. $stmt->execute();
  114.  
  115. // Check for successful insertion
  116. if ( $stmt->affected_rows ) {
  117. return true;
  118. }
  119.  
  120. return false;
  121. }
  122. public function select($query, $data, $format) {
  123. // Connect to the database
  124. $db = $this->connect();
  125.  
  126. //Prepare our query for binding
  127. $stmt = $db->prepare($query);
  128.  
  129. //Normalize format
  130. $format = implode('', $format);
  131. $format = str_replace('%', '', $format);
  132.  
  133. // Prepend $format onto $values
  134. array_unshift($data, $format);
  135.  
  136. //Dynamically bind values
  137. call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
  138.  
  139. //Execute the query
  140. $stmt->execute();
  141.  
  142. //Fetch results
  143. $result = $stmt->get_result();
  144.  
  145. //Create results object
  146. while ($row = $result->fetch_object()) {
  147. $results[] = $row;
  148. }
  149.  
  150. return $results;
  151. }
  152. public function delete($table, $id) {
  153. // Connect to the database
  154. $db = $this->connect();
  155.  
  156. // Prepary our query for binding
  157. $stmt = $db->prepare("DELETE FROM {$table} WHERE ID = ?");
  158.  
  159. // Dynamically bind values
  160. $stmt->bind_param('d', $id);
  161.  
  162. // Execute the query
  163. $stmt->execute();
  164.  
  165. // Check for successful insertion
  166. if ( $stmt->affected_rows ) {
  167. return true;
  168. }
  169. }
  170. private function prep_query($data, $type='insert') {
  171. // Instantiate $fields and $placeholders for looping
  172. $fields = '';
  173. $placeholders = '';
  174. $values = array();
  175.  
  176. // Loop through $data and build $fields, $placeholders, and $values
  177. foreach ( $data as $field => $value ) {
  178. $fields .= "{$field},";
  179. $values[] = $value;
  180.  
  181. if ( $type == 'update') {
  182. $placeholders .= $field . '=?,';
  183. } else {
  184. $placeholders .= '?,';
  185. }
  186.  
  187. }
  188.  
  189. // Normalize $fields and $placeholders for inserting
  190. $fields = substr($fields, 0, -1);
  191. $placeholders = substr($placeholders, 0, -1);
  192.  
  193. return array( $fields, $placeholders, $values );
  194. }
  195. private function ref_values($array) {
  196. $refs = array();
  197.  
  198. foreach ($array as $key => $value) {
  199. $refs[$key] = &$array[$key];
  200. }
  201.  
  202. return $refs;
  203. }
  204. }
  205. }
  206.  
  207. $db = new DB('root', '', 'test');
  208. print_r($db->get_results("SELECT * FROM objects"));
  209.  
  210. ?>
Add Comment
Please, Sign In to add comment