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

0

Hello. After hours trying to solve this problem and research I did about the problem, I came to ask you how I can remedy this issue.

The error is:

  

Fatal error: Call to a member function result () on a non-object in /home/iaulas/public_html/system/application/models/site_model.php on line 483

I removed the error line to see if this error occurred somewhere else and on another line the same error occurs.

This is the function: (I use the codeigniter framework)

        function cat_busca() { 
        $query = 'SELECT materiais_cat_id as id, MCCat as cat, MCOrdem as ordem FROM ((SELECT * FROM materiais_cat where materiais_cat.MCMostrar = "Sim" GROUP BY MCCat) UNION (SELECT * FROM livros_cat WHERE livros_cat.LCMostrar = "Sim" GROUP BY LCCat) UNION (SELECT * FROM cursos_cat WHERE cursos_cat.CCMostrar = "Sim" GROUP BY CCCat)) AS tabela ORDER BY ordem';
        $retorno = $this->db->query($query);
        return $retorno->result();
    }

I could not post the whole code because there are almost 4 thousand lines.

In addition to help to fix, I would like to understand why this error occurred because it happened only after I changed the hosting site.

Thank you!

    
asked by anonymous 17.12.2015 / 04:34

1 answer

1

This error is caused by your SQL command line that is in the $query variable. I've been through this problem many times until I got it back if there was a line at least in that query.

To avoid this type of problem, it is advisable to always check the number of rows returned, thus leaving your code:

function cat_busca() { 
        $query = 'SELECT materiais_cat_id as id, MCCat as cat, MCOrdem as ordem FROM ((SELECT * FROM materiais_cat where materiais_cat.MCMostrar = "Sim" GROUP BY MCCat) UNION (SELECT * FROM livros_cat WHERE livros_cat.LCMostrar = "Sim" GROUP BY LCCat) UNION (SELECT * FROM cursos_cat WHERE cursos_cat.CCMostrar = "Sim" GROUP BY CCCat)) AS tabela ORDER BY ordem';
        $retorno = $this->db->query($query);
if($retorno->num_rows() > 0){
        return $retorno->result();
}else{
return false;
}
}

The error

  

Fatal error: Call a member function result () on a non-object in   /home/iaulas/public_html/system/application/models/site_model.php on   line 483

It means that the function you call result () is not an object, that is, it has no record and you are forcing it to print an object it does not have.

Always check the number of rows to see if any rows have been returned. If you think this error is strange and you would have to return something, then check your command SQL of $ query , maybe the error is there.

    
17.12.2015 / 04:47