Advertisement
Guest User

serviceUser.php

a guest
Aug 13th, 2017
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2.  
  3. class ServiceUser{
  4.  
  5.     private $db;
  6.     private $user;
  7.  
  8.     function __construct(Mysqli $conn,User $user){
  9.  
  10.         $this->db = $conn;
  11.         $this->user = $user;
  12.  
  13.  
  14.     }
  15.  
  16.     public function find($id){
  17.  
  18.         $stmt = $this->db->stmt_init();
  19.         $stmt->prepare("SELECT * FROM  {$this->user->getTable()} WHERE id = ?");
  20.         $stmt->bind_param("i",$id);
  21.         $stmt->execute();
  22.         $stmt->bind_result($id,$name,$email);
  23.         $stmt->fetch();
  24.         return array("id"=>$id,"name"=>$name,"email"=>$email);
  25.  
  26.     }
  27.  
  28.     public function list($order = null){
  29.  
  30.         if($order){
  31.             $sql = "SELECT * FROM {$this->user->getTable()} ORDER BY {$order}";
  32.         }else{
  33.             $sql = "SELECT * FROM {$this->user->getTable()}";
  34.         }
  35.         $query = $this->db->query($sql);
  36.         return $query->fetch_all(MYSQLI_ASSOC);
  37.  
  38.     }
  39.  
  40.     public function insert(){
  41.  
  42.         $stmt = $this->db->stmt_init();
  43.         $stmt->prepare("INSERT INTO {$this->user->getTable()} (name,email,password) VALUES (?,?,?)");
  44.         $name = $this->user->getName();
  45.         $email = $this->user->getEmail();
  46.         $password = $this->user->getPassword();
  47.         $stmt->bind_param("sss",$name,$email,$password);
  48.         $stmt->execute();
  49.         return $stmt->insert_id;
  50.  
  51.     }
  52.  
  53.     public function update(){
  54.  
  55.         $stmt = $this->db->stmt_init();
  56.         $stmt->prepare("UPDATE {$this->user->getTable()} SET name = ?, email= ?, password= ? WHERE id = ?");
  57.         $name = $this->user->getName();
  58.         $email = $this->user->getEmail();
  59.         $password = $this->user->getPassword();
  60.         $id = $this->user->getId();
  61.         $stmt->bind_param("sssi",$name,$email,$password,$id);
  62.         return $stmt->execute();
  63.  
  64.     }
  65.  
  66.     public function delete($id){
  67.  
  68.         $stmt = $this->db->stmt_init();
  69.         $stmt->prepare("DELETE FROM {$this->user->getTable()} WHERE id = ?");
  70.         $stmt->bind_param("i",$id);
  71.         return $stmt->execute();
  72.  
  73.     }
  74.  
  75. }
  76.  
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement