Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 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. public function update($table, $data, $format, $where, $where_format) {
  62. // Check for $table or $data not set
  63. if ( empty( $table ) || empty( $data ) ) {
  64. return false;
  65. }
  66.  
  67. // Connect to the database
  68. $db = $this->connect();
  69.  
  70. // Cast $data and $format to arrays
  71. $data = (array) $data;
  72. $format = (array) $format;
  73.  
  74. // Build format array
  75. $format = implode('', $format);
  76. $format = str_replace('%', '', $format);
  77. $where_format = implode('', $where_format);
  78. $where_format = str_replace('%', '', $where_format);
  79. $format .= $where_format;
  80.  
  81. list( $fields, $placeholders, $values ) = $this->prep_query($data, 'update');
  82.  
  83. //Format where clause
  84. $where_clause = '';
  85. $where_values = '';
  86. $count = 0;
  87.  
  88. foreach ( $where as $field => $value ) {
  89. if ( $count > 0 ) {
  90. $where_clause .= ' AND ';
  91. }
  92.  
  93. $where_clause .= $field . '=?';
  94. $where_values[] = $value;
  95.  
  96. $count++;
  97. }
  98.  
  99. // Prepend $format onto $values
  100. array_unshift($values, $format);
  101. $values = array_merge($values, $where_values);
  102.  
  103. // Prepary our query for binding
  104. $stmt = $db->prepare("UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");
  105.  
  106. // Dynamically bind values
  107. call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
  108.  
  109. // Execute the query
  110. $stmt->execute();
  111.  
  112. // Check for successful insertion
  113. if ( $stmt->affected_rows ) {
  114. return true;
  115. }
  116.  
  117. return false;
  118. }
  119. public function select($query, $data, $format) {
  120. // Connect to the database
  121. $db = $this->connect();
  122.  
  123. //Prepare our query for binding
  124. $stmt = $db->prepare($query);
  125.  
  126. //Normalize format
  127. $format = implode('', $format);
  128. $format = str_replace('%', '', $format);
  129.  
  130. // Prepend $format onto $values
  131. array_unshift($data, $format);
  132.  
  133. //Dynamically bind values
  134. call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
  135.  
  136. //Execute the query
  137. $stmt->execute();
  138.  
  139. //Fetch results
  140. $result = $stmt->get_result();
  141.  
  142. //Create results object
  143. while ($row = $result->fetch_object()) {
  144. $results[] = $row;
  145. }
  146.  
  147. return $results;
  148. }
  149. public function delete($table, $id) {
  150. // Connect to the database
  151. $db = $this->connect();
  152.  
  153. // Prepary our query for binding
  154. $stmt = $db->prepare("DELETE FROM {$table} WHERE ID = ?");
  155.  
  156. // Dynamically bind values
  157. $stmt->bind_param('d', $id);
  158.  
  159. // Execute the query
  160. $stmt->execute();
  161.  
  162. // Check for successful insertion
  163. if ( $stmt->affected_rows ) {
  164. return true;
  165. }
  166. }
  167. private function prep_query($data, $type='insert') {
  168. // Instantiate $fields and $placeholders for looping
  169. $fields = '';
  170. $placeholders = '';
  171. $values = array();
  172.  
  173. // Loop through $data and build $fields, $placeholders, and $values
  174. foreach ( $data as $field => $value ) {
  175. $fields .= "{$field},";
  176. $values[] = $value;
  177.  
  178. if ( $type == 'update') {
  179. $placeholders .= $field . '=?,';
  180. } else {
  181. $placeholders .= '?,';
  182. }
  183.  
  184. }
  185.  
  186. // Normalize $fields and $placeholders for inserting
  187. $fields = substr($fields, 0, -1);
  188. $placeholders = substr($placeholders, 0, -1);
  189.  
  190. return array( $fields, $placeholders, $values );
  191. }
  192. private function ref_values($array) {
  193. $refs = array();
  194.  
  195. foreach ($array as $key => $value) {
  196. $refs[$key] = &$array[$key];
  197. }
  198.  
  199. return $refs;
  200. }
  201. }
  202. }
  203.  
  204. $db = new DB('root', '', 'test');
  205. print_r($db->select('SELECT * FROM objects WHERE ID = ?', array(10), array('%d')));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement