Fatal error: Call to a member function num_rows ()

1

I have the following lines in CodeIgniter : Line 217

    if ($query->num_rows() == 0)
    {
        $this->sess_destroy();
        return FALSE;
    }

Displays the following error:

  

Fatal error: Call a member function num_rows () on a non-object in   file.php on line 217

What can it be? Previously it worked perfectly ... I just reinstalled the machine and now it has this problem. I'm using CodeIgniter and EasyPHP for testing.

If I change the line by if (count($query) == 0) , it will work fine, however, so I would have to change everywhere I use num_rows , and inside the server it works right.

    
asked by anonymous 21.11.2015 / 20:57

1 answer

0

What is happening is that the $ query variable most likely received the false return value of the query in the database. When you try to access the num_rows () method it says that you are trying to access a method of a non-object variable. As in PHP a variable has its type defined at runtime, $ query can now be an object and now can be Boolean. You can call var_dump($query) to see what amount it received.

To prevent the error from being thrown on the user's face, you can do the following sequence of checks:

try {
    $query = ...;
    if ($query === false) {
        throw new Exception();
    } else if ($query->num_rows() < 1) {
        return 0;
    }
    /* ESCREVA AQUI A LÓGICA DE TRATAMENTO DOS DADOS */

} catch (Exception $e) {
    return null;
}

I hope I have helped. :)

    
04.05.2016 / 06:23