MYSQLI - Trying to get Property of non-object in

Asked

Viewed 2,801 times

0

I have the following function:

function sql_update_views ($table = null, $id = null) {
    $database = open_database();
    $found = null;
    try {
        $sql = "UPDATE ".$table." SET views = views + 1 WHERE id = ".$id;
        $result = $database -> query($sql);
        if ($result -> num_rows > 0) {
            $found = $result -> fetch_all(MYSQLI_ASSOC);
        }
    }
    catch (Exception $e) {
        $_SESSION['message'] = $e -> GetMessage();
        $_SESSION['type'] = 'danger';
    }
    close_database($database);
    return $found;
}

I get the following error:

Notice: Trying to get Property of non-object in D: xampp htdocs armazemdahouse include database.php on line 167

If I apply the solution contained in the question:

Proposed solution

I get the error:

Notice: Undefined variable: num_rows in D: xampp htdocs armazemdahouse include database.php on line 167

What is the solution?

I realized that if the user accesses the page and keeps pressing F5 then the views of the publication will increase in a non cordial way. Where I should do the treatment so that this type of situation is blocked, by IP in another table?

  • 1

    Read this: https://pt.meta.stackoverflow.com/a/5510/132

1 answer

1


You are getting this error because you are trying to call the function num_rows() on top of a query UPDATE.

If you want to recover the amount of lines that have been updated with the UPDATE, utilize affected_rows().

Addendum: you will only be able to execute this code snippet $result -> fetch_all(MYSQLI_ASSOC); if in the $result variable a objeto or a array of a consultation SELECT to the database. There is no way you can "recover data" from a UPDATE.

And according to the PHP documentation:

mysqli_affected_rows() returns the number of rows affected by the last query INSERT, UPDATE, REPLACE or DELETE associated with the link parameter specified. If the last query was invalid, this function will return -1.

The behaviour of mysqli_num_rows() depends on whether buffered or Unbuffered result sets are being used. For Unbuffered result sets, mysqli_num_rows() will not Return the correct number of Rows until all the Rows in the result have been retrieved.


As suggested by our colleague, there goes a small-better implementation of your code.

Note: there are numerous things that could be modified, but I will keep your logic and knowledge written in the code.

try {
    $sql   = "UPDATE " . $table . " SET views = views + 1 WHERE id = " . $id;
    $query = $database->query($sql);

    // Retorno rápido.
    if ( ! $query) {
        throw new Exception("Oh my god, um erro."); 
    }

    $updatedRows = $query->affected_rows(MYSQLI_ASSOC);

    if ($updatedRows == 0) {
        throw new Exception("Nenhuma row foi atualizada.");           
    }

    // Retorna a quantidade de *rows* atualizadas.
    return $updatedRows;
}
catch (Exception $e) {
    $_SESSION['message'] = $e->getMessage();
    $_SESSION['type'] = 'danger';
}
  • Make some recommendations to him in his answer: don’t use space between the object, -> and the method or property; use getMessage() and not GetMessage(); and finally test the execution of query with if ($result === TRUE) or if($result).

  • 1

    @Tonymontana Tà lá ;)

  • Your answer was already very good, but thank you for following my recommendation. I had already voted in your reply.

  • @Godfreytheking thanks for your help, I’ll implement here. you could show which changes I could make to my code to make it more correct or safe or functional, please?

Browser other questions tagged

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