Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. <?php
  2.  
  3. // Simple PDO wrapper to ease database interactions, always returning multi-dimensional array for consistency
  4. class PDOWrapper
  5. {
  6. public $dbc;
  7.  
  8.  
  9. // Establish database connection
  10. public function __construct($db_host, $db_name, $db_user, $db_pass)
  11. {
  12. try {
  13. $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  14. return $this->dbc;
  15. }
  16. catch (Exception $error) {
  17. return $error;
  18. }
  19. } // END constructor method
  20.  
  21.  
  22. // Force multi-dimensional array
  23. public function return_array($data)
  24. {
  25. $return_array = [];
  26. return $return_array['data'] = $data;
  27. } // END return_array method
  28.  
  29.  
  30. // TODO: Build try/catch method?
  31.  
  32.  
  33. // Get table columns
  34. public function getColumns($table)
  35. {
  36. try {
  37. $stmt = $this->dbc->prepare("SELECT * FROM " . $table);
  38. $stmt->execute();
  39. $array = $stmt->fetch(PDO::FETCH_ASSOC);
  40. }
  41. catch (Exception $error) {
  42. return $error;
  43. }
  44.  
  45. // Parse through returned results and build array of table columns
  46. $columns_array = [];
  47. foreach ($array as $column => $field) {
  48. $columns_array[] = $column;
  49. }
  50.  
  51. // Return multi-dimensional array
  52. $this->return_array($columns_array);
  53. } // END getColumns method
  54.  
  55.  
  56. // Show tables in database
  57. public function getTables()
  58. {
  59. $stmt = $this->dbc->prepare("SHOW TABLES;");
  60. $stmt->execute();
  61. $results = $stmt->fetch();
  62.  
  63. // Return multi-dimensional array
  64. $this->return_array($results);
  65. } // END getTables method
  66.  
  67.  
  68. // Query database dynamically
  69. public function select($table, $where_assoc_array = [], $limit = 'display_all')
  70. {
  71. // Handle default 'display_all' value given for human reliability
  72. if ($limit == 'display_all') {
  73. $limit = '';
  74. }
  75. else {
  76. $limit = ' LIMIT ' . $limit;
  77. }
  78.  
  79. // Build 'where' clause
  80. if (!empty($where_assoc_array)) {
  81. $where_clause = 'WHERE ';
  82. foreach ($where_assoc_array as $column => $field) {
  83. $where_clause .= $column . " = " . $field . " AND ";
  84. }
  85.  
  86. // Trim off extra 'AND' off end of string
  87. rtrim($where_clause, ' AND ');
  88. }
  89. else {
  90. $where_clause = '';
  91. }
  92.  
  93. try {
  94. // Build statement
  95. $stmt = $this->dbc->prepare("SELECT * FROM" . $table . $where_clause . $limit . ";");
  96. $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  97.  
  98. // Return multi-dimensional array
  99. return $this->return_array($results);
  100. }
  101. catch (Exception $error) {
  102. return $this->return_array($error);
  103. }
  104. } // END select method
  105.  
  106.  
  107. // Insert into database dynamically
  108. public function insert($table, $assoc_array)
  109. {
  110. $columns_array = [];
  111. $values_array = [];
  112.  
  113. // Parse array given
  114. foreach ($assoc_array as $key => $value) {
  115. $columns_array[] = $key;
  116. $values_array[] = $value;
  117. }
  118.  
  119. // Build insert statement
  120. $stmt = $this->dbc->prepare("INSERT INTO" . $table . " (" . $columns_array . ") VALUES (" . $values_array . ");");
  121. $stmt->execute();
  122. $results = $stmt->fetch();
  123.  
  124. // Return multi-dimensional array
  125. $this->return_array($results);
  126. } // END insert method
  127.  
  128.  
  129. // Update values dynamically
  130. public function update($table, $assoc_array, $where_assoc_array = [])
  131. {
  132. $stmt_middle = '';
  133.  
  134. // Parse array given
  135. foreach ($assoc_array as $key => $value) {
  136. $stmt_middle .= $key . " = '" . $value . "', ";
  137. }
  138.  
  139. // Trim off extra comma
  140. rtrim($stmt_middle, ', ');
  141.  
  142. // Build statement
  143. $stmt_begin = "UPDATE" . $table . "SET ";
  144. $stmt_end = "WHERE ";
  145.  
  146. // Parse array to build 'WHERE' clause
  147. foreach ($where_assoc_array as $key => $value) {
  148. $stmt_end .= $key . " = " . $value . " AND ";
  149. }
  150. // Trim off extra 'AND' off end of string
  151. rtrim($stmt_end, ' AND ');
  152.  
  153. try {
  154. // Build update statement
  155. $stmt = $this->dbc->prepare($stmt_begin . $stmt_middle . $stmt_end);
  156. $stmt->execute();
  157. $results = $stmt->fetch();
  158.  
  159. // Return multi-dimensional array
  160. $this->return_array($results);
  161. }
  162. catch (Exception $error) {
  163. $this->return_array($error);
  164. }
  165. } // END update method
  166.  
  167.  
  168. // Delete rows from table
  169. public function delete($table, $assoc_array)
  170. {
  171. // TODO:
  172. } // END delete method
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement