【问题标题】:Call to member function num_rows() on non object using codeigniter使用 codeigniter 在非对象上调用成员函数 num_rows()
【发布时间】:2013-10-24 07:02:17
【问题描述】:

我被这个愚蠢的问题困住了好几次,我知道我可能犯了一些愚蠢的错误,基本上我有一个控制器,它调用模型函数并返回数据,然后将它分配到一个数组中,但是我在非对象成员错误时继续调用成员函数 num_rows,

我的控制器功能是:

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;
    }

}

错误出现在调用 num_rows 函数的 else 部分...

【问题讨论】:

  • 那么可能 $this->db->get() 返回false 而不是一个对象。在$this-&gt;db-&gt;get() 之后放置一个var_dump($this-&gt;db-&gt;last_query()) 并检查SQL 语句
  • 我不确定$this-&gt;db-&gt;get()是否返回一个对象
  • 我同意帕特里克的观点。如果未检查查询结果是否成功,这种情况很常见。
  • 如果它返回 FALSE,那么控件应该转到 return 语句的 true 部分而不是转到 else 部分。它正在返回一些东西,并且使用 var_dump 我找到了 bool(true),它我没有回来...

标签: php mysql codeigniter


【解决方案1】:

试试这个:

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 );
}

【讨论】:

  • 这将 return 一个空数组,因为您正在初始化 $data 但您从未向其中填充任何内容:P
  • 是的,我知道这一点,这始终是初始化数组并返回它的好选择,而不是我认为的 OP 正在做的事情。这样做,您将不会遇到 OP 所面临的任何此类错误。您将始终确定输出可能是什么。 @PatrickManser
  • 查看@RoyalBg 的回答。您对其进行初始化,从不向其中填充任何内容,然后将其返回为空。
  • 我的错,$query 应该是 $data,现在,没关系。 :P @PatrickManser
  • @blogger:试试这个,让我知道。
【解决方案2】:

使用关注

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;
    }

}

【讨论】:

    【解决方案3】:

    如果您陷入for 循环,请尝试使用foreach。这是我的最新答案:

    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";
            }
    }
    

    和模型:

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

    【讨论】:

    • 如果它返回 FALSE,那么控件应该转到 return 语句的 true 部分而不是转到 else 部分。它正在返回一些东西,并且使用 var_dump 我找到了 bool(true),它我没有回来...
    • result() 更改为result_array()。但是您必须将$row-&gt;FY_budget_ID 更改为$row['FY_budget_ID'],因为它将是一个数组。我很抱歉:D
    • 怎么替换` if ($query->num_rows() == 0) { return FALSE; } 其他 { 返回 $ 查询; }` 在模型中,我认为这里不需要。只是return $this-&gt;db-&gt;get_where('fy_budget','client_block_ID' =&gt; $client_block_ID);。我会更新答案
    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2011-12-23
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多