Fatal error: Call to a Member Function num_rows() on a non-object

Asked

Viewed 1,133 times

1

I’m looking for the total number of lines of a query but keeps giving this error, I’ve tried to use the function Row(), rowCount() and even check if found any results to return and still not reached the solution

Fatal error: Call to a Member Function num_rows() on a non-object in C: wamp www topsamp-ci application models Servor_model.php on line 86

public function getVotosHoje($idServer){
    $data = date('Y-m-d');
    $select = array('id', 'data');
    $where = "idServidor = '$idServer' AND data = '$data'";
    $this->db->where($where);
    $this->db->select($select);
    $retorno = $this->db->get('votos')->result();
    if($retorno->num_rows() > 0){
        return $retorno->num_rows();
    } else{
        return false;
    }
}

1 answer

5


You are wrong result() does not return a class with functions, it returns the data, therefore it does not have the function num_rows():

$retorno = $this->db->get('votos')->result();
if($retorno->num_rows() > 0){
    return $retorno->num_rows();
} else{
    return false;
}

It’s right to use it like this:

$total = $this->db->get('votos')->num_rows();

if($total > 0){
    return $total;
} else{
    return false;
}

Now if what you want is to take the records if it’s bigger than 0 then do so:

$retorno = $this->db->get('votos');

if($retorno->num_rows() > 0){ //Conta registros
    return $retorno->result(); //Retorna os registros
} else{
    return false; //Retorna false se tiver 0 registros
}
  • It worked, thank you!

  • @Viniciusaquino please mark the answer as accepted.

  • 1

    Puts dude, it worked but only returns me the last record of the table, to calling this method inside the foreach passing the id of each item: <?= $this->server->getVotosHoje($v->id); ?>

  • @Viniciusaquino I think I understood your need, I edited the answer, take a look again.

  • It didn’t work, it only returns me 2 records

  • @Viniciusaquino the problem is in your query probably because you limited by date $data = date('Y-m-d');, will only appear today’s records. Anything try like this $retorno = $this->db->get();&#xA;&#xA;if($retorno->num_rows() > 0){&#xA; return $retorno->result();&#xA;}

  • 1

    I’m sorry, I was looking at the data on the dashboard with the wrong account, it’s working normally, thanks for the help!

Show 2 more comments

Browser other questions tagged

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