CodeIgniter Forums
Fatal error: Cannot use object of type CI_DB_mysql_result as array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Fatal error: Cannot use object of type CI_DB_mysql_result as array (/thread-22364.html)



Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-06-2009

[eluser]MEM[/eluser]
Fatal error: Cannot use object of type CI_DB_mysql_result as array ... on line 81.

I was having this on line 81:
Code:
if($intParentId == $arrRows[$i]['parent_id_cat']) {

I now have this:
Code:
if($intParentId == $arrRows[$i]->parent_id_cat) {

Same error occurs. Sad Any help?





Regards,
Márcio


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-06-2009

[eluser]brianw1975[/eluser]
It's not in the if or the assignment, you have to look deeper. Look at the code that is creating $arrRows


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]MEM[/eluser]
I believe that is the first time that I treat him as an array...

Here is the full code:

Code:
private function _parseResultTree($intParentId,&$arrRows,$intDepth,$intRunner)
     {
        
        // stop at this depth
        if($intDepth == $intRunner)
        {
            return array();
        }
    
        $arrChildren = array();

        for($i=0;$i<count($arrRows);$i++)
        {
            if($intParentId == $arrRows[$i]->parent_id_cat) {
                $arrChildren = array_merge($arrChildren,array_splice($arrRows,$i--,1));
            }
        }
    
        $intChildren = count($arrChildren);
        if($intChildren != 0)
        {
            for($i=0;$i<$intChildren;$i++)
            {
                $arrChildren[$i]['children'] = parse_into_tree($arrChildren[$i]['id_cat'],$arrRows,$intDepth,$intRunner++);
            }        
        }
    
        return $arrChildren;
    
    }


thanks a lot,
Márcio


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]Aken[/eluser]
Your $arrRows is an object, not an array. Either change the $arrRows into an array, or use $arrRows->$i->parent_id_cat.


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]MEM[/eluser]
That will not work since $i is a numeric index, and $0 or $1 or $n..., will not be properties of query returned object.


Sad

Márcio


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]Aken[/eluser]
Can't help you further without knowing what $arrRows looks like going into the function. The error itself is saying that you're trying to use an object as an array. So you should turn your $arrRows into an array, then use your original IF statement.


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]MEM[/eluser]
Thanks again.

Sorry, I thought I have post it all...

I'm not using result() or anything, I'm just using the absolutly fundamental query(). It happens that this query returns an object.

Should I grab this returned query object, and use result_array() to transform into an array, and then use this code again?

Isn't there an easy way for changing the for loop to have the same effect, but working with objects?




Here is the full code:

Code:
public function getSubCategorias($id_cat,$intDepth=2)
{    
        
    $strSQL = 'SELECT c.id_cat,c.nome_cat,c.parent_id_cat FROM categoria c';
    $arrQuery = $this->db->query($strSQL);
    return $this->_parseResultTree($id_cat,$arrQuery,$intDepth, $intRunner=0);
    
}


private function _parseResultTree($intParentId,&$arrRows,$intDepth,$intRunner)
{
        
        // stop at this depth
        if($intDepth == $intRunner)
        {
            return array();
        }
    
        $arrChildren = array();

        for($i=0;$i<count($arrRows);$i++)
        {
            if($intParentId == $arrRows[$i]->parent_id_cat) {
                $arrChildren = array_merge($arrChildren,array_splice($arrRows,$i--,1));
            }
        }
    
        $intChildren = count($arrChildren);
        if($intChildren != 0)
        {
            for($i=0;$i<$intChildren;$i++)
            {
                $arrChildren[$i]['children'] = parse_into_tree($arrChildren[$i]['id_cat'],$arrRows,$intDepth,$intRunner++);
            }        
        }
    
        return $arrChildren;
}

Please advice,
Márcio


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]MEM[/eluser]
I have:

1) grab the query() object.

2) Use the result_array() method:


Works.

Here the steps:
Code:
$query = $this->db->query($strSQL);
        
$arrQuery = $query->result_array();


Thanks a lot for the tip. Smile


Fatal error: Cannot use object of type CI_DB_mysql_result as array - El Forum - 09-07-2009

[eluser]Aken[/eluser]
Bingo Smile Cool.