Guest User

Untitled

a guest
Feb 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4. // Database connection
  5. private $servername = 'localhost';
  6. private $username = 'root';
  7. private $password = '';
  8. private $dbname = 'emp';
  9.  
  10. function __construct()
  11. {
  12. $this->db = new mysqli(
  13. $this->servername,
  14. $this->username,
  15. $this->password,
  16. $this->dbname
  17. );
  18.  
  19. if ($this->db->connect_error) {
  20. die("Connection failed: " . $this->db->connect_error);
  21. }
  22. }
  23.  
  24. public function insert($table, $cols, $vals) { // insert function
  25.  
  26. $colsString = implode(',', $cols);
  27. $valsString = implode(',', $vals);
  28.  
  29. $sql = "INSERT INTO $table ($colsString) VALUES ($valsString)";
  30. if ($this->db->query($sql) === TRUE) {
  31. // return $this->Db->insert_id;
  32. return true;
  33. } else {
  34. return "Error: " . $sql . "<br>" . $this->db->error;
  35. }
  36. }
  37.  
  38. public function select($table,$cols = array()) { // select function
  39. if(empty($cols)){
  40. $colsString = '*';
  41. }else{
  42. $colsString = implode(',', $cols);
  43. }
  44. $sql="select $colsString from $table";
  45. $result=$this->db->query($sql);
  46. while($row = $result->fetch_assoc()) {
  47. $result1[] = $row;
  48. }
  49. // $row = mysqli_fetch_all($result);
  50. return $result1;
  51. }
  52. }
  53.  
  54. ?>
Add Comment
Please, Sign In to add comment