Fatal error: Call to a Member Function num_rows()

Asked

Viewed 202 times

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 to a Member Function num_rows() on a non-object in php file on line 217

What can it be? It previously worked perfectly... Only I reinstalled the machine and now it’s given this problem. I’m using the Codeigniter and the Easyphp for testing.

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

  • post the variable line $query

  • This line was taken from Session.php of codeigniter, in this situation I am checking if the user exists... however, if I change as I said in the question, use Count, it works... but I would have to use Count in everything...

  • For starters, $query->num_rows() is different from count($query). First understand what you’re doing, you’re just trying everything without even understanding what’s going on. Your database connection is working properly?

  • Yes, it is normal...

1 answer

0


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

To prevent the error from being thrown in the user’s face, you can make 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’ve helped. :)

Browser other questions tagged

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