MYSQLI - Trying to get property of non-object in

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 \ wareemdahouse \ include \ database.php on line 167

If you apply the solution contained in the question:

Solution proposal

I get the error:

  

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

What is the solution?

I noticed that if the user accesses the page and keeps pressing the F5 then the views of the publication will increase in an unfriendly way. Where should I make the treatment to block this type of situation by IP in another table?

    
asked by anonymous 24.08.2017 / 23:18

1 answer

2

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

If you want to retrieve the number of rows that were updated with UPDATE , use affected_rows() .

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

And according to PHP documentation :

  

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

     

The behavior 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, here's a little better implementation of your code.

Note: There are many 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';
}
    
24.08.2017 / 23:43