简体   繁体   中英

Call to member function num_rows() on non object using codeigniter

I am stuck at this silly problem, for quite a few times and i know i might have done some silly mistake, basically i have a controller which calls a model function and returns data and then assign it in an array, but i keep getting call to member function num_rows on non object member error,

My controller function is:

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    if ($alreadyExist == FALSE) 
    {
      // code if nothing found
    } else 
    {
     for ($i=0; $i < $alreadyExist->num_rows(); $i++) 
     { 
       $row = $alreadyExist->row($i);
       $FY_budget_ID[$i] = $row->FY_budget_ID;
     }
    }
}

My model is :

public function checkAlreadyExist($client_block_ID)
{
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $query = $this->db->get('fy_budget');
    if ($query->num_rows() >= 1) 
    {
        return $query;
    } else 
    {
        return FALSE;
    }

}

The error is in the else part where num_rows function is called...

if you get stucked in for loop, try to use foreach . It is my updated answer :

public function registerNewLease()
{
        $this->load->model('AnnualBudget');
        $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
        if ($alreadyExist != NULL){
            $i = 0;
            foreach ($alreadyExist->result_array() as $row){
            $i++;
            $FY_budget_ID[$i] = $row['FY_budget_ID'];
            }
        }
        else{
    //no result here
         echo "no result";
        }
}

and the model:

function checkAlreadyExist($client_block_ID)
{
     return $this->db->get_where('fy_budget','client_block_ID' => $client_block_ID);
}

Try this:

public function checkAlreadyExist($client_block_ID)
{
    $data   = array();
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $data   = $this->db->get('fy_budget')->result_array();
    return $data;
}

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $FY_budget_ID = array();
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    echo $this->db->last_query();
    /*if( isset( $alreadyExist ) && count( $alreadyExist ) > 0 ){
        foreach ( $alreadyExist as $key => $each ) 
        { 
            $FY_budget_ID[$key] = $each['FY_budget_ID'];
        }    
    }*/
    echo "<pre>";print_r( $alreadyExist );
}

use following

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    if ($alreadyExist == FALSE) 
    {
      // code if nothing found
    } else 
    {
     for ($i=0; $i < $this->db->count_all_results(); $i++) 
     { 
       $row = $alreadyExist->row($i);
       $FY_budget_ID[$i] = $row->FY_budget_ID;
     }
    }
}

My model is :

public function checkAlreadyExist($client_block_ID)
{
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $query = $this->db->get('fy_budget');
    if ($this->db->count_all_results() >= 1) 
    {
        return $query;
    } else 
    {
        return FALSE;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM