How to remove the fatal error when fetching an assoc array

The variable $stmt is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn’t have a method “fetch_assoc()” defined for it.

You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method. For this you need the mysqlInd driver installed!

$result = $stmt->get_result();
row = $result->fetch_assoc();

If you don’t have the driver installed you can fetch your results like this:

$stmt->bind_result($dbUser, $dbEmail);
while ($stmt->fetch()) {
    printf("%s %s\n", $dbUser, $dbEmail);
}

So your code should become:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// bind variables to result
$stmt->bind_result($dbUser, $dbEmail);
//fetch the first result row, this pumps the result values in the bound variables
if($stmt->fetch()){
    echo 'result is ' . dbEmail;
}

Leave a Comment