在DB_Functions中调用未定义的方法mysqli_stmt :: get_result()

时间:2017-11-29 21:37:32

标签: php mysql database

我有一个PHP文件,其中有一个函数用于检查用户输入 - 输出但不知何故不起作用,当我调试它然后出现错误,因为调用未定义的方法mysqli_stmt :: get_result' 这是功能代码

 public function getUserByEmailAndPassword($email, $password) {

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

       $stmt->bind_param("s", $email);

     if ($stmt->execute()) {
         $user = $stmt->get_result()->fetch_assoc();
         $stmt->close();


        // verifying user password
        $salt = $user['salt'];
        $encrypted_password = $user['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) {
            // user authentication details are correct
            return $user;
        }
    } else {
        return NULL;
    }
}

我在我的托管服务提供商的服务器上没有启用mysqlnd,我现在无法启用它,因为托管公司不能提供SSL证书,所以必须找到一些替代方法同样也不要改变结果,我已经尝试了这个Call to undefined method mysqli_stmt::get_result答案,但没有运气,因为我对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() {

}

/**
 * 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->bind_param("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 = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $user = $stmt->get_result()->fetch_assoc();
        $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 = ?");

       $stmt->bind_param("s", $email);

     if ($stmt->execute()) {
         $user = $stmt->get_result()->fetch_assoc();
         $stmt->close();


        // verifying user password
        $salt = $user['salt'];
        $encrypted_password = $user['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) {
            // user authentication details are correct
            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 = ?");

    $stmt->bind_param("s", $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;
}  } ?>

使用上述功能检查条件的登录文件

<?php require_once 'include/DB_Functions.php';

$ db = new DB_Functions(); $ response = array(&#34; error&#34; =&gt; FALSE);

if(isset($ _ POST [&#39; email&#39;])&amp;&amp;&amp; isset($ _ POST [&#39;密码&#39;])){

$email = $_POST['email'];
$password = $_POST['password'];

// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);

if ($user != false) {
    // use is found
    $response["error"] = FALSE;
    $response["uid"] = $user["unique_id"];
    $response["user"]["name"] = $user["name"];
    $response["user"]["email"] = $user["email"];
    $response["user"]["created_at"] = $user["created_at"];
    $response["user"]["updated_at"] = $user["updated_at"];
    echo json_encode($response);
} else {
    // user is not found with the credentials
    $response["error"] = TRUE;
    $response["error_msg"] = "Login credentials are wrong. Please try again!";
    echo json_encode($response);
}

}?&gt;

与此同时,我尝试使用PDO将mysqli从等式中移除,但这也是回归没有任何疑问

 public function getUserByEmailAndPassword($email, $password) {
         $dsn = 'mysql:host=hostname; dbname=dbname';
         $user = username;
         $password = password;
        $db= new PDO($dsn, $user, $password);

          $params = array(':email' => $email,':password'=>$password );

          $stm = $db->prepare('SELECT * FROM users WHERE email= :email AND password= :password');
         if($stm->execute($params)){
          $user = $stm->fetch(PDO::FETCH_ASSOC);

        // verifying user password
        $salt = $user['salt'];
        $encrypted_password = $user['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) {
            // user authentication details are correct
            return $user;
        }
    } else {
        return NULL;
    }
}

1 个答案:

答案 0 :(得分:0)

get_result仅适用于本机驱动器(mysqlnd)

改为使用store_result

http://php.net/manual/en/mysqli.store-result.php