Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ServiceUser{
- private $db;
- private $user;
- function __construct(Mysqli $conn,User $user){
- $this->db = $conn;
- $this->user = $user;
- }
- public function find($id){
- $stmt = $this->db->stmt_init();
- $stmt->prepare("SELECT * FROM {$this->user->getTable()} WHERE id = ?");
- $stmt->bind_param("i",$id);
- $stmt->execute();
- $stmt->bind_result($id,$name,$email);
- $stmt->fetch();
- return array("id"=>$id,"name"=>$name,"email"=>$email);
- }
- public function list($order = null){
- if($order){
- $sql = "SELECT * FROM {$this->user->getTable()} ORDER BY {$order}";
- }else{
- $sql = "SELECT * FROM {$this->user->getTable()}";
- }
- $query = $this->db->query($sql);
- return $query->fetch_all(MYSQLI_ASSOC);
- }
- public function insert(){
- $stmt = $this->db->stmt_init();
- $stmt->prepare("INSERT INTO {$this->user->getTable()} (name,email,password) VALUES (?,?,?)");
- $name = $this->user->getName();
- $email = $this->user->getEmail();
- $password = $this->user->getPassword();
- $stmt->bind_param("sss",$name,$email,$password);
- $stmt->execute();
- return $stmt->insert_id;
- }
- public function update(){
- $stmt = $this->db->stmt_init();
- $stmt->prepare("UPDATE {$this->user->getTable()} SET name = ?, email= ?, password= ? WHERE id = ?");
- $name = $this->user->getName();
- $email = $this->user->getEmail();
- $password = $this->user->getPassword();
- $id = $this->user->getId();
- $stmt->bind_param("sssi",$name,$email,$password,$id);
- return $stmt->execute();
- }
- public function delete($id){
- $stmt = $this->db->stmt_init();
- $stmt->prepare("DELETE FROM {$this->user->getTable()} WHERE id = ?");
- $stmt->bind_param("i",$id);
- return $stmt->execute();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement