0

I'm working in this project that is an Android login app. It fetches data from an online server.

The registration works and the database gets updated but the login does not work. We are using JSON to parse data from the server to the app.

Here is my PHP code:

public function getUserByEmailAndPassword($email, $password) {

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

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

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

The error I'm getting is the following:

Fatal error: Call to a member function fetch_assoc() on a non-object.
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
  • 3
    Well, the documentation says, `$stmt->bind_result()` returns a `bool`. Therefore the error message is not surprising. You cannot call a method on a bool, since a bool is not an object. http://php.net/manual/en/mysqli-stmt.bind-result.php – arkascha Oct 07 '15 at 16:24
  • You can't use `bind_result()` here. `bind_result()` lets you assign variables to each column returned, you are doing `SELECT *`, so that won't work. If you have MySQLnd, then you can use `->get_result()`. See: http://php.net/manual/en/mysqli-stmt.get-result.php – gen_Eric Oct 07 '15 at 16:29
  • **$stmt->fetch_assoc();** shoud be done after binding result variables. – Subin Thomas Oct 07 '15 at 16:30

1 Answers1

1

bind_result() is not the method you want here. That method (which returns a boolean, by the way) lets you assign variables to the columns returned in your query. Your query uses SELECT *, so you cannot use bind_result().

What you want is to use get_result(). This requires the mysqlnd driver to be installed.

$user = $stmt->get_result()->fetch_assoc();
gen_Eric
  • 223,194
  • 41
  • 299
  • 337