Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. class DB{
  2. private $dbHost = "localhost";
  3. private $dbUsername = "root";
  4. private $dbPassword = "";
  5. private $dbName = "computer network";
  6.  
  7. public function __construct(){
  8. if(!isset($this->db)){
  9. // Connect to the database
  10. $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
  11. if($conn->connect_error){
  12. die("Failed to connect with MySQL: " . $conn->connect_error);
  13. }else{
  14. $this->db = $conn;
  15. }
  16. }
  17. }
  18.  
  19. /*
  20. * Returns rows from the database based on the conditions
  21. * @param string name of the table
  22. * @param array select, where, order_by, limit and return_type conditions
  23. */
  24. public function getRows($table,$conditions = array()){
  25. $sql = 'SELECT ';
  26. $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
  27. $sql .= ' FROM '.$table;
  28.  
  29.  
  30. if(array_key_exists("where",$conditions)){
  31. $sql .= ' WHERE ';
  32. $i = 0;
  33. foreach($conditions['where'] as $key => $value){
  34. $pre = ($i > 0)?' AND ':'';
  35. $sql .= $pre.$key." != '".$value."'";
  36. $i++;
  37. }
  38. }
  39.  
  40. if(array_key_exists("order_by",$conditions)){
  41. $sql .= ' ORDER BY '.$conditions['order_by'];
  42. }
  43.  
  44. if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
  45. $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
  46. }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
  47. $sql .= ' LIMIT '.$conditions['limit'];
  48. }
  49.  
  50. $result = $this->db->query($sql);
  51.  
  52. if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
  53. switch($conditions['return_type']){
  54. case 'count':
  55. $data = $result->num_rows;
  56. break;
  57. case 'single':
  58. $data = $result->fetch_assoc();
  59. break;
  60. default:
  61. $data = '';
  62. }
  63.  
  64. }else{
  65. if($result->num_rows > 0){
  66. while($row = $result->fetch_assoc()){
  67. $data[] = $row;
  68. }
  69. }
  70. }
  71. return !empty($data)?$data:false;
  72. }
  73.  
  74. /*
  75. * Insert data into the database
  76. * @param string name of the table
  77. * @param array the data for inserting into the table
  78. */
  79. public function insert($table,$data){
  80. if(!empty($data) && is_array($data)){
  81. $columns = '';
  82. $values = '';
  83. $i = 0;
  84. if(!array_key_exists('created',$data)){
  85. $data['created'] = date("Y-m-d H:i:s");
  86. }
  87. if(!array_key_exists('modified',$data)){
  88. $data['modified'] = date("Y-m-d H:i:s");
  89. }
  90. foreach($data as $key=>$val){
  91. $pre = ($i > 0)?', ':'';
  92. $columns .= $pre.$key;
  93. $values .= $pre."'".$val."'";
  94. $i++;
  95. }
  96. $query = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
  97. $insert = $this->db->query($query);
  98. return $insert?$this->db->insert_id:false;
  99. }else{
  100. return false;
  101. }
  102. }
  103.  
  104. /*
  105. * Update data into the database
  106. * @param string name of the table
  107. * @param array the data for updating into the table
  108. * @param array where condition on updating data
  109. */
  110. public function update($table,$data,$conditions){
  111. if(!empty($data) && is_array($data)){
  112. $colvalSet = '';
  113. $whereSql = '';
  114. $i = 0;
  115. if(!array_key_exists('modified',$data)){
  116. $data['modified'] = date("Y-m-d H:i:s");
  117. }
  118. foreach($data as $key=>$val){
  119. $pre = ($i > 0)?', ':'';
  120. $colvalSet .= $pre.$key."='".$val."'";
  121. $i++;
  122. }
  123. if(!empty($conditions)&& is_array($conditions)){
  124. $whereSql .= ' WHERE ';
  125. $i = 0;
  126. foreach($conditions as $key => $value){
  127. $pre = ($i > 0)?' AND ':'';
  128. $whereSql .= $pre.$key." = '".$value."'";
  129. $i++;
  130. }
  131. }
  132. $query = "UPDATE ".$table." SET ".$colvalSet.$whereSql;
  133. $update = $this->db->query($query);
  134. return $update?$this->db->affected_rows:false;
  135. }else{
  136. return false;
  137. }
  138. }
  139.  
  140. /*
  141. * Delete data from the database
  142. * @param string name of the table
  143. * @param array where condition on deleting data
  144. */
  145. public function delete($table,$conditions){
  146. $whereSql = '';
  147. if(!empty($conditions)&& is_array($conditions)){
  148. $whereSql .= ' WHERE ';
  149. $i = 0;
  150. foreach($conditions as $key => $value){
  151. $pre = ($i > 0)?' AND ':'';
  152. $whereSql .= $pre.$key." = '".$value."'";
  153. $i++;
  154. }
  155. }
  156. $query = "DELETE FROM ".$table.$whereSql;
  157. $delete = $this->db->query($query);
  158. return $delete?true:false;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement