Sachinthana Aluvihare
Sachinthana Aluvihare

Reputation: 273

Fatal error: Call to a member function fetch_assoc() on a non-object

How do I fix this error? This is my code.

$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");

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

if ($stmt->execute()) {
    $user = $stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type)->fetch_assoc();
    $stmt->close();

I'm using a remote SQL database. I have also tried this on my localhost. But then I get the error:

fatal error call to a member function fetch_assoc() on boolean

This code worked with get_result instead of bind_result. But I need to use bind_result.

Upvotes: 1

Views: 278

Answers (2)

Nelson Owalo
Nelson Owalo

Reputation: 2414

You can't chain bind_result as it doesn't return an object. Try this:

$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");

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

if ($stmt->execute()) {
    $stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type);
    $user = $stmt->fetch();
    $stmt->close();

Note: When using mysqli_stmt_execute(), the mysqli_stmt_fetch() function must be used to fetch the data prior to performing any additional queries.

Upvotes: 0

Julie Pelletier
Julie Pelletier

Reputation: 1716

bind_result() does not return the object but instead a status. This means that it can not be chained to another method call from that object. You need to do it on two separate lines.

See the bind_result manual page for details.

Upvotes: 1

Related Questions