Advertisement
Guest User

DAO_User.php

a guest
Feb 10th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.     header("Content-type: text/html; charset=UTF-8");
  3.     include_once $_SERVER['DOCUMENT_ROOT']."/MI/Model/Beans/User.php";
  4.    
  5.     //include ("../Beans/User.php");
  6.    
  7.     include_once 'DAO.php';
  8.    
  9.    
  10.    
  11.     class DAO_User extends DAO {
  12.                
  13.         /* *****************************************************
  14.          * constructor method
  15.          ******************************************************/
  16.         public function __construct () {
  17.             parent::__construct();
  18.         }
  19.  
  20.         /* *****************************************************
  21.          * interfaces methods
  22.          ******************************************************/
  23.         public function create($obj){
  24.             $this->connection = Database::getInstance()->OpenConnection();
  25.             $object_class = get_class($obj);
  26.             $sql = "INSERT INTO ".$object_class."(name, user_name, password, email, idAdministrator) VALUES (?, ?, ?, ?, ?);";
  27.             $statement = $this->connection->prepare($sql);
  28.            
  29.             if (!$statement) {
  30.                 echo 'erro na consulta: '. $this->connection->errno .' - '. $this->connection->error;
  31.             }
  32.            
  33.             $statement->bind_param(ssssi,$obj->getName(), $obj->getUser_Name(), $obj->getPassword, $obj->getEmail, $obj->getIdAdministrator);
  34.             $statement->execute();
  35.            
  36.             $statement->close();
  37.             $this->connection->close();
  38.         }
  39.        
  40.         public function read() {
  41.             return null;
  42.         }
  43.         public function readById($id) { return null; }
  44.         public function update($obj) { return null; }
  45.         public function delete($obj) { return null; }
  46.        
  47.         /* *****************************************************
  48.          * specific methods
  49.          ******************************************************/
  50.         public function login($userName, $password){
  51.             $this->connection = Database::getInstance()->OpenConnection();
  52.             $sql = "select * from user where user_name = ? and password = ?;";
  53.            
  54.             $statement = mysqli_prepare($this->connection, $sql);
  55.            
  56.             //if any error happen in connection
  57.             if (!$statement) {
  58.                 echo 'erro na consulta: '. $this->connection->errno .' - '. $this->connection->error;
  59.             }
  60.            
  61.             //associate the user name and password with sql parameters,
  62.             //execute the query and store the result
  63.             $statement->bind_param('ss', $userName,$password);
  64.             $statement->execute();
  65.             $result = $statement->get_result();
  66.                
  67.             $user;
  68.            
  69.             //create and set the user variable
  70.             while($row = mysqli_fetch_array($result)){
  71.                 $user = new User($row["idUser"], $row["name"], $row["email"], $row["user_name"], $row["password"], $row["type"], $row["active"], $row["idAdministrator"]);
  72.             }
  73.            
  74.             echo "teste: ".$user->name;
  75.    
  76.             //close statement and connection
  77.             $statement->close();
  78.             Database::getInstance()->CloseConnection($this->connection);
  79.            
  80.             return $user;
  81.         }
  82.        
  83.        
  84.     }
  85.  
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement