Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * @Author Rian (godamri@gmail.com)
  4. */
  5. class Database
  6. {
  7. /*
  8. * Database credentials
  9. */
  10. private $host = '127.0.0.1';
  11. private $user = 'root';
  12. private $database = 'illusion_sample';
  13. private $password = '';
  14.  
  15. /*
  16. * Other private stuff
  17. */
  18. private $connection=null;
  19.  
  20. function __construct()
  21. {
  22. /*
  23. * Test Connection
  24. */
  25. try{
  26. $this->connection = new mysqli($this->host, $this->user, $this->password, $this->database);
  27. if ($this->connection->connect_errno) {
  28.  
  29. /*
  30. * Throw error message
  31. */
  32. throw new Exception($this->connection->connect_error);
  33.  
  34. }
  35. }
  36. catch (Exception $e){
  37.  
  38. echo "<h1> Error estabilishing database connection : " . $e->getMessage() . "</h1>";
  39.  
  40. }
  41. }
  42. function __destruct()
  43. {
  44.  
  45. $this->connection->close();
  46.  
  47. }
  48.  
  49. public function getAll($table=null)
  50. {
  51. /*
  52. * Set default table name same as db name
  53. */
  54. $table = ( $table == null ) ? $this->database : $table;
  55. $q = "SELECT * FROM $table";
  56. $d = array();
  57. $result = $this->connection->query($q);
  58. if ($result->num_rows > 0)
  59. {
  60. while ($row = $result->fetch_assoc()) {
  61. $d[] = $row;
  62. }
  63. }
  64. if (count($result)>0) {
  65. return $d;
  66. }
  67. return false;
  68. }
  69.  
  70. public function getOne($table=null,$where=null)
  71. {
  72. if ( $where == null) return false;
  73.  
  74. /*
  75. * Set default table name same as db name
  76. */
  77. $table = ( $table == null ) ? $this->database : $table;
  78.  
  79. $q = "SELECT * FROM $table WHERE $where LIMIT 1";
  80. $result = $this->connection->query($q);
  81. if ($result) {
  82. return $result->fetch_assoc();
  83. }
  84. return false;
  85. }
  86.  
  87. public function update($table=null,$where=null,$data=null)
  88. {
  89. if ( $where == null) return false;
  90. if ( $data == null) return false;
  91. $update = '';
  92. /*
  93. * Set default table name same as db name
  94. */
  95. $table = ( $table == null ) ? $this->database : $table;
  96.  
  97. /*
  98. * Build received data into query
  99. */
  100. foreach ($data as $key => $value) {
  101. $update .="`$key` = '$value',";
  102. }
  103. $update = rtrim($update, ",");
  104.  
  105. $q = "UPDATE $table SET $update WHERE $where";
  106. $result = $this->connection->query($q);
  107. if ($result) {
  108. return true;
  109. }
  110. return false;
  111. }
  112.  
  113. public function delete($table=null,$where=null)
  114. {
  115. if ( $where == null) return false;
  116.  
  117. /*
  118. * Set default table name same as db name
  119. */
  120. $table = ( $table == null ) ? $this->database : $table;
  121. if ($where == 'DROP' )
  122. {
  123. $q = "DELETE FROM $table";
  124. }
  125. else {
  126. $q = "DELETE FROM $table WHERE $where";
  127. }
  128. $result = $this->connection->query($q);
  129. if ($result) {
  130. return true;
  131. }
  132. return false;
  133. }
  134.  
  135. public function insert($table=null,$data=null)
  136. {
  137. if ( $data == null) return false;
  138. $fields = '';
  139. $values = '';
  140.  
  141. /*
  142. * Set default table name same as db name
  143. */
  144.  
  145. $table = ( $table == null ) ? $this->database : $table;
  146.  
  147. /*
  148. * Build received data into query
  149. */
  150. foreach ($data as $key => $value) {
  151. $value = mysqli_real_escape_string($this->connection,$value);
  152. $fields .="`$key`,";
  153. $values .="'$value',";
  154. }
  155.  
  156. $fields = "(" . rtrim($fields,',') . ")";
  157. $values = "(" . rtrim($values,',') . ")";
  158. $q = "INSERT INTO $table $fields VALUES $values";
  159. $result = $this->connection->query($q);
  160. if ($result) {
  161. return $this->connection->insert_id;
  162. }
  163. return false;
  164. }
  165.  
  166.  
  167. public function getAllBy($table=null,$where=null,$order=null,$limit=null)
  168. {
  169. /*
  170. * Set default table name same as db name
  171. */
  172. $table = ( $table == null ) ? $this->database : $table;
  173. $where = ( $where == null ) ? "" : "WHERE " . $where;
  174. $order = ( $order == null ) ? "" : " ORDER BY " . $order;
  175. $limit = ( $limit == null ) ? " LIMIT 0,30 " : " LIMIT " . $limit;
  176.  
  177. $q = "SELECT * FROM $table $where $order $limit";
  178. $d = array();
  179. $result = $this->connection->query($q);
  180. if (isset($result->num_rows) AND $result->num_rows > 0)
  181. {
  182. while ($row = $result->fetch_assoc()) {
  183. $d[] = $row;
  184. }
  185. }
  186. if (count($result)>0) {
  187. return $d;
  188. }
  189. return false;
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement