Fatal error: Call a member function num_rows () on a non-object

0

I'm trying to get the total number of rows from a query but I'm giving this error, I already tried to use the row (), rowCount () function and even check if I found any results to return and even though I did not get the solution

  

Fatal error: Call a member function num_rows () on a non-object in   C: \ wamp \ www \ topsamp-ci \ application \ models \ model-server.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;
    }
}
    
asked by anonymous 24.01.2017 / 22:59

1 answer

4

Is wrong result() does not return a class with functions, it returns the data, so 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;
}

The correct way is to use this:

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

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

Now if what you want is to get the records if it is greater 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
}
    
24.01.2017 / 23:07