2015-10-17 148 views
0

我想做一個登錄android系統,當我嘗試登錄我得到錯誤"undefined function consigueResultado"在74行,但我宣佈函數。有人知道可能是錯的嗎? 我正在使用000webhost可能與此錯誤有任何關係?謝謝:)Php函數出現未聲明

<?php 
class DB_Functions { 

    private $conn; 

    // constructor 
    function __construct() { 
     require_once 'DB_Connect.php'; 
     // connecting to database 
     $db = new Db_Connect(); 
     $this->conn = $db->connect(); 
    } 

    // destructor 
    function __destruct() { 

    } 

    public function consigueResultado($stmt) { 
     $RESULT = array(); 
     $stmt->store_result(); 
     for ($i = 0; $i < $stmt->num_rows; $i++) { 
      $Metadata = $stmt->result_metadata(); 
      $PARAMS = array(); 
      while ($Field = $Metadata->fetch_field()) { 
       $PARAMS[] = &$RESULT[ $i ][ $Field->name ]; 
      } 
      call_user_func_array(array($stmt, 'bind_result'), $PARAMS); 
      $stmt->fetch(); 
     } 
     return $RESULT; 
    } 

    /** 
    * Storing new user 
    * returns user details 
    */ 
    public function storeUser($name, $email, $password) { 
     $uuid = uniqid('', true); 
     $hash = $this->hashSSHA($password); 
     $encrypted_password = $hash["encrypted"]; // encrypted password 
     $salt = $hash["salt"]; // salt 

     $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())"); 
     $stmt->bindParam("sssss", $uuid, $name, $email, $encrypted_password, $salt); 
     $result = $stmt->execute(); 
     $stmt->close(); 

     // check for successful store 
     if ($result) { 
      $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email"); 
      $stmt->bindParam(':email', $email); 
      $stmt->execute(); 
      $user = consigueResultado($stmt); 
      $stmt->close(); 

      return $user; 
     } else { 
      return false; 
     } 
    } 

    /** 
    * Get user by email and password 
    */ 
    public function getUserByEmailAndPassword($email, $password) { 

     $stmt = $this->conn->prepare("SELECT * FROM users WHERE email =:email"); 

     $stmt->bindParam(':email', $email); 

     if ($stmt->execute()) { 
      $user = consigueResultado($stmt); 
      $stmt->close(); 
      return $user; 
     } else { 
      return NULL; 
     } 
    } 

    /** 
    * Check user is existed or not 
    */ 
    public function isUserExisted($email) { 
     $stmt = $this->conn->prepare("SELECT email from users WHERE email = :email"); 

     $stmt->bindParam(':email', $email); 
     $stmt->execute(); 

     $stmt->store_result(); 

     if ($stmt->num_rows > 0) { 
      // user existed 
      $stmt->close(); 
      return true; 
     } else { 
      // user not existed 
      $stmt->close(); 
      return false; 
     } 
    } 

    /** 
    * Encrypting password 
    * @param password 
    * returns salt and encrypted password 
    */ 
    public function hashSSHA($password) { 

     $salt = sha1(rand()); 
     $salt = substr($salt, 0, 10); 
     $encrypted = base64_encode(sha1($password . $salt, true) . $salt); 
     $hash = array("salt" => $salt, "encrypted" => $encrypted); 
     return $hash; 
    } 

    /** 
    * Decrypting password 
    * @param salt, password 
    * returns hash string 
    */ 
    public function checkhashSSHA($salt, $password) { 

     $hash = base64_encode(sha1($password . $salt, true) . $salt); 

     return $hash; 
    } 
} 
?> 
+2

您可以在類中定義函數。這意味着你必須使用類對象來訪問它,例如:'$ this-> consigueResultado(...)' – arkascha

+0

是的。就像阿卡沙說的那樣。 –

回答

2

函數稱爲方法。方法不存在於全局範圍中,而是存在於對象的作用域中。通過閱讀Classes and Objects更好地理解這一點。這樣做的直接影響是,你不應該試圖調用功能 consigueResultado,而是像這樣的方法:

$user = $this->consigueResultado($stmt); 
3

,我們必須使用這個關鍵字$呼叫當前類的方法內部類,例如你的方法調用應該像這樣的類

$this->consigueResultado($stmt); 
0

你可以嘗試使用打電話的self功能。試試這個self::consigueResultado(..);

2

請爲該類創建一個對象,之後您可以調用該函數。

$obj = new Yourclassname(); 
$obj->yourfunction(); 

就像這個...

0

你爲什麼用 「:電子郵件」,而不是 「s」 嗎?

「:email」聲明在哪裏?