How to fix error num_rows Property

Asked

Viewed 29 times

1

Folks I have a question regarding this error:

Notice: Trying to get Property 'num_rows' of non-object in C: xampp htdocs control inc database.php on line 94.

Follows part of the code.

function find( $table = null, $id_tag = null ) {            
    $database = open_database();        
    $found = null;          

    try {         
        if ($id_tag) {         
             $sql = "SELECT * FROM " . $table . " WHERE id= " . $id_tag;            
             $result = $database->query($sql);                      

             if ($result->num_rows > 0) {             
                 $found = $result->fetch_assoc();           
                }                     
            } else {                       
                 $sql = "SELECT * FROM " . $table;          
                $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;  
}
  • 1

    that mistake means that $result does not contain property num_rows i.e., the query is returning some error attempts follow this example take care to close the result set after the query

  • Basically the query failed because the table or the field does not exist. As far as I see in the parameters can be null what makes SQL potentially select * from null where id = null. Do var_dump($sql) before the execution of query to see exactly the command that is running and realize the problem.

No answers

Browser other questions tagged

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