Error "Trying to get property of non-object" when using mysqli_stmt_fetch

0

I have the following error: "Trying to get property of non-object on line 22" in the following code:

<?php
require('config.php');


if (isset($_POST['email']))
{
    $email = stripslashes($_REQUEST['email']);

    $email = mysqli_real_escape_string($conn,$email);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($conn,$password);

    $stmt = $conn->prepare("SELECT password FROM registo WHERE email=?");
    $stmt->bind_param("s",$email);
    $email = $_POST['email'];
    $stmt->execute();
    $result= $stmt->store_result();

    if($stmt->affected_rows > 0)
    {    
       $user = mysqli_stmt_bind_result($stmt, $pass);
        if(password_verify($password,$pass))
        {
            $_SESSION['email'] = $email;
            $_SESSION['user'] = true;
            header("Location: home.php");

Same error:

<?php while($books =mysqli_stmt_fetch($stmt)){?> 
        <?php $id = $id +1;?> 
            <tr> 

                <td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $books->Image).'"height="100" width="100">'; ?> </td> //linha 92

I have tried several ways and I can not solve it.

    
asked by anonymous 24.05.2017 / 21:15

1 answer

1

The mysqli_stmt_fetch function, or the fetch method of the stmt object, has the following form:

bool mysqli_stmt::fetch ( void )    // Método fetch do objeto stmt
bool mysqli_stmt_fetch ( mysqli_stmt $stmt )    // Função mysqli_stmt_fetch 

Notice that return is a Boolean value, not an object. You did:

$books = mysqli_stmt_fetch($stmt)

And tried to access the value by $book->Image . That is, you tried to access the Image attribute of a Boolean value. So the mistake. The return of the function / method is:

  • TRUE if the data was successfully fetched;
  • FALSE if an error occurred;
  • NULL , when there are no more records;

Considering that you have executed the following query:

SELECT password FROM registo WHERE email=?

To get the value of $password , you should do:

if($stmt->affected_rows > 0)
{   
    // Associa o valor retornado na busca à variável $pass:
    $stmt->bind_result($pass);

    // Enquanto houverem registros:
    while ($stmt->fetch())
    {
        // Exibe o valor retornado da consulta:
        echo $pass;
    }
}

If you prefer, you can use procedural mode (you would not have to because the rest of your code is OOP):

if($stmt->affected_rows > 0)
{   
    // Associa o valor retornado na busca à variável $pass:
    mysqli_stmt_fetch($stmt, $pass);

    // Enquanto houverem registros:
    while (mysqli_stmt_fetch($stmt))
    {
        // Exibe o valor retornado da consulta:
        echo $pass;
    }
}

In your second code, where you are selecting books, you will have to define a variable for each column returned from the database:

if($stmt->affected_rows > 0)
{   
    $stmt->bind_result($id, $title, $author, $image, ...);

    while ($stmt->fetch())
    {
        // Exibe os dados do livro:
        echo $id, $title, $author, $image, ...;
    }
}
    
25.05.2017 / 02:52