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

Asked

Viewed 490 times

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 mistake:

<?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’ve tried many ways and I can’t seem to solve.

  • But the function mysqli_stmt_fetch returns bool, not an object.

  • how do I return an object? sorry I’m new at this

  • Try replacing line 21 with mysqli_stmt_bind_result($stmt, $pass) and on line 22 instead of using $user->password, use $pass only.

  • That line $email = $_POST['email']; must be before: $stmt->bind_param("s",$email);

  • Anderson with his suggestion and with the rray modified my code. With Anderson’s suggestion there is no more error, however it says that the email/password are wrong. This is code that of a log in form. before I applied the bind Parameter gave no error and entered with the credentials. but now even though I’m not making any mistakes, it says that credentials aren’t right and that’s not true!

  • You generated the value stored in the database with the function password_hash? If yes, is there any way to verify exactly what values are arriving in the condition? Use var_dump the two variables and a die to finalize the request.

  • Anderson, I think the password problem already solved. but now suggested the same problem "Trying to get Property of non-object" on line 92. In WHILE I tried to apply the same solution that Voce suggested earlier but still gives the same error. PF see edited the code to add the remaining code where

  • Gives the same error because you did the same wrong way. The function does not return an object, you need to pass the desired variables by parameter. In the first comment I Linkei the documentation of the function, please. read it.

Show 3 more comments

1 answer

0

The function mysqli_stmt_fetch, or the method fetch of the object stmt, 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 

Note that the 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, tried to access the attribute Image of a boolean value. Hence the error. The return of the function/method is:

  • TRUE, whether the data have been successfully obtained;
  • FALSE, if an error occurred;
  • NULL, when there are no more records;

Whereas you executed the following query:

SELECT password FROM registo WHERE email=?

To obtain the value of $password, you must 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 (there would be no reason since 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 the books, you will have to set 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, ...;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.