0

I am searching for hours for an alternative to function get_result() in PHP.

THis is the code:

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;
    }
}

But I am getting an error at line:

$user = $stmt->get_result()->fetch_assoc();
Barmar
  • 741,623
  • 53
  • 500
  • 612
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • What error are you getting? – Barmar Mar 19 '17 at 07:15
  • 1
    Read the documentation: This function is only available with the `mysqlnd` driver. – Barmar Mar 19 '17 at 07:16
  • @Barmar, this is the error: Call to undefined method mysqli_stmt::get_result() – mvasco Mar 19 '17 at 07:17
  • http://stackoverflow.com/questions/10466530/mysqli-prepared-statement-unable-to-get-result – Ankur Jyoti Phukan Mar 19 '17 at 07:17
  • 1
    If you don't have that driver, you need to use `bind_result()` to bind the result columns to variables, you can't use `fetch_assoc()` with prepared statements. – Barmar Mar 19 '17 at 07:17
  • @Barmar, I have checked the question you say I am duplicating, but I am not able to make it work on my case.Would you mind to give me an example of bind_result()? – mvasco Mar 19 '17 at 07:27
  • 1
    Change your query to `SELECT encrypted_password FROM ...`. Then do `$stmt->bind_result($encrypted_password); $stmt->fetch();`. – Barmar Mar 19 '17 at 07:31
  • The best alternative is PDO – Your Common Sense Mar 19 '17 at 07:34
  • @Barmar, I have changed the query for: $stmt = $this->conn->prepare("SELECT encrypted_password,salt FROM users WHERE email = ?"); – mvasco Mar 19 '17 at 07:43
  • @Barmar, and then I have added: $user = $stmt->bind_result($encrypted_password,$salt); $stmt->fetch(); Now I am not getting the error, but the credentials are not passed ok. – mvasco Mar 19 '17 at 07:45
  • `bind_result()` doesn't return anything useful to assign to `$user`. If you want to return other columns in an associative array, you'll need to add them to the `SELECT` list, and build the array yourself. If you want generic code to do this, follow some of the related links in the duplicate question. – Barmar Mar 19 '17 at 14:52

0 Answers0